How to interact with scene objects for non-master clients?

I have a script, wich allows player to grab and throw game objects, but in photon multiplayer it only works for master client. What i am trying to do is to give permission for other players to use this script, when they try to grab objects. I tried to do it similarly to how it is done in photon demo "ChangeOwner", but it does not work.I have an empty game object in the scene, wich has this script attached to it:
 using UnityEngine;
 public class DemoOwnershipGui : MonoBehaviour
 {
     public GUISkin Skin;
     public bool TransferOwnershipOnRequest = true;
     public void OnOwnershipRequest(object[] viewAndPlayer)
     {
         PhotonView view = viewAndPlayer[0] as PhotonView;
         PhotonPlayer requestingPlayer = viewAndPlayer[1] as PhotonPlayer;
         Debug.Log("OnOwnershipRequest(): Player " + requestingPlayer + " requests ownership of: " + view + ".");
         if (this.TransferOwnershipOnRequest)
         {
             view.TransferOwnership(requestingPlayer.ID);
         }
      } 
}
And here is the script, attached to player prefab: (i tried using [punRPC] with functions, but probably did something wrong, also i can not give carry() function the carriedObject parameter using RPC)
using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 [RequireComponent( typeof( PhotonView ) )]
 public class PickupObjectNetwork : Photon.MonoBehaviour {
 GameObject mainCamera;
 public bool carrying;
 GameObject carriedObject;
 public float distance;
 public float smooth;
 public float throwPower = 10f;
 private float chargeTime = 0f;
 public Slider throwIndicator;
 // Use this for initialization
 void Start () {
     mainCamera = GameObject.FindWithTag("MainCamera");
 }

 // Update is called once per frame
 void Update () {
     if(photonView.isMine)
     {
         if(carrying) {
             carry(carriedObject);
             checkDrop();

             if (Input.GetMouseButtonUp (0)) {
                 float pushForce = chargeTime * 10;
                 pushForce = Mathf.Clamp (pushForce, 1, 20);
                 throwObject(pushForce);
                 chargeTime = 0;
             }
             rotateObject();
         } else {
             pickup();
         }
         if(Input.GetMouseButton(0)){
             chargeTime += Time.deltaTime;
             throwIndicator.value = chargeTime * 20;
         }
     }
 }
 void rotateObject() {
     carriedObject.transform.Rotate(90,90,90);
 }
 void carry(GameObject o) {        
     if (carriedObject == null)
         pickup();
     o.transform.position = Vector3.Lerp (o.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth);
     o.transform.rotation = Quaternion.identity;
 }
 void pickup() {        
     if(Input.GetKeyDown (KeyCode.E)) {
         if( this.photonView.ownerId != PhotonNetwork.player.ID )
         {
             Debug.Log( "Not requesting ownership. Already mine." );
             //    this.photonView.RequestOwnership();
         }
         else 
             this.photonView.RequestOwnership();

         int x = Screen.width / 2;
         int y = Screen.height / 2;
         Ray ray = mainCamera.GetComponent().ScreenPointToRay(new Vector3(x,y));
         RaycastHit hit;
         if(Physics.Raycast(ray, out hit)) {
             Pickupable p = hit.collider.GetComponent();
             if(p != null && hit.distance < 5) {
                 carrying = true;
                 carriedObject = p.gameObject;
                 //p.gameObject.rigidbody.isKinematic = true;
                 p.gameObject.GetComponent().useGravity = false;
             }
         }
     }
 }
 void checkDrop() {        
     if(Input.GetKeyDown (KeyCode.E)) {
         dropObject();
     }
 }

 void dropObject() {
     carrying = false;
     //carriedObject.gameObject.rigidbody.isKinematic = false;
     carriedObject.gameObject.GetComponent().useGravity = true;
     carriedObject = null;
 }
 void throwObject(float pushForce){    
     carrying = false;
     if(carriedObject.tag != "Box")
         carriedObject.tag = "Projectile";
     carriedObject.GetComponent ().AddForce(Camera.main.transform.forward * pushForce * 200);
     carriedObject.gameObject.GetComponent ().useGravity = true;            
     carriedObject = null;
 }    
 }

Comments

  • GiViZedGiViZed
    edited November 2016
    I changed this code to:
    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    [RequireComponent( typeof( PhotonView ) )]
    public class PickupObjectNetwork : Photon.MonoBehaviour {
    	GameObject mainCamera;
    	public bool carrying;
    	GameObject carriedObject;
    	public float distance;
    	public float smooth;
    	public float throwPower = 10f;
    	private float chargeTime = 0f;
    	public Slider throwIndicator;
    
    	// Use this for initialization
    	void Start () {
    		mainCamera = GameObject.FindWithTag("MainCamera");	
    	}
    	
    	// Update is called once per frame
    	void Update () {
    		if(photonView.isMine)
    		{
    			if(carrying) {
    				photonView.RPC("carry", PhotonTargets.All, carriedObject.GetComponent().viewID);
    				photonView.RPC("checkDrop", PhotonTargets.All);
    	
    				if (Input.GetMouseButtonUp (0)) {
    					float pushForce = chargeTime * 10;
    					pushForce = Mathf.Clamp (pushForce, 1, 20);
    					photonView.RPC("throwObject", PhotonTargets.All, pushForce);
    					chargeTime = 0;
    				}				
    			} else {
    				photonView.RPC("pickup", PhotonTargets.All);
    			}
    
    			if(Input.GetMouseButton(0)){
    				chargeTime += Time.deltaTime;
    			}
    		}
    	}
    
    	[PunRPC]
    	void carry(int id) {			
    		if (carriedObject == null)
    			pickup();
    		GameObject o = PhotonView.Find(id).gameObject;
    		o.transform.position = Vector3.Lerp (o.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth);
    		o.transform.rotation = Quaternion.identity;
    	}
    
    	[PunRPC]
    	void pickup() {		
    		if(Input.GetKeyDown (KeyCode.E)) {			
    			int x = Screen.width / 2;
    			int y = Screen.height / 2;
    
    			Ray ray = mainCamera.GetComponent().ScreenPointToRay(new Vector3(x,y));
    			RaycastHit hit;
    			if(Physics.Raycast(ray, out hit)) {
    				Pickupable p = hit.collider.GetComponent();
    				if(p != null && hit.distance < 5) {
    					carrying = true;
    					carriedObject = p.gameObject;					
    					p.gameObject.GetComponent().useGravity = false;					
    					this.photonView.RPC("addTime", PhotonTargets.AllViaServer, p.gameObject.GetComponent().timeOut);
    				}
    			}
    		}
    	}
    	[PunRPC]
    	void checkDrop() {			
    		if(Input.GetKeyDown (KeyCode.E)) {
    			dropObject();
    		}
    	}
    	[PunRPC]
    	void dropObject() {
    		carrying = false;		
    		carriedObject.gameObject.GetComponent().useGravity = true;
    		carriedObject = null;
    	}
    	[PunRPC]
    	void throwObject(float pushForce){			
    		carrying = false;
    		if(carriedObject.tag != "BoxMultiplayer")
    			carriedObject.tag = "Projectile";
    		carriedObject.GetComponent ().AddForce(Camera.main.transform.forward * pushForce * 200);
    		carriedObject.gameObject.GetComponent ().useGravity = true;			
    		carriedObject = null;
    	}	
    }
    But is still does not work for non-master players! I don`t understand why, the object i try to grab moves a little, but then it looks like there is another force, which pulls it back to place and it starts to "jitter". I really need some help
  • using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    
    [RequireComponent( typeof( PhotonView ) )]
    public class PickupObjectNetwork : Photon.MonoBehaviour {
    	GameObject mainCamera;	
    	public bool carrying;
    	GameObject carriedObject;
    	public float distance;
    	public float smooth;
    	public float throwPower = 10f;
    	private float chargeTime = 0f;	
    	
    	void Start () {
    		mainCamera = GameObject.FindWithTag("MainCamera");			
    	}
    	
    	void Update () {
    		if(pView.isMine)
    		{
    			if(carrying) {
    				photonView.RPC("carry", PhotonTargets.AllBuffered, carriedObject.GetComponent().viewID);				
    				photonView.RPC("checkDrop", PhotonTargets.AllBuffered);
    	
    				if (Input.GetMouseButtonUp (0)) {
    					float pushForce = chargeTime * 10;
    					pushForce = Mathf.Clamp (pushForce, 1, 20);
    					photonView.RPC("throwObject", PhotonTargets.AllBuffered, pushForce);
    					chargeTime = 0;
    				}
    				
    			} else {
    				if(Input.GetKeyDown (KeyCode.E)) {
    					int viewID = PhotonNetwork.AllocateViewID();
    					photonView.RPC("pickup", PhotonTargets.AllBuffered, viewID);
    				}
    			}
    
    			if(Input.GetMouseButton(0)){
    				chargeTime += Time.deltaTime;
    			}
    		}
    	}
    
    	[PunRPC]
    	void carry(int id) {			
    		if (carriedObject == null)
    		{
    			if(Input.GetKeyDown (KeyCode.E)) 
    			{
    				int viewID = PhotonNetwork.AllocateViewID();
    				photonView.RPC("pickup", PhotonTargets.AllBuffered, viewID);
    			}
    		}
    		GameObject o = PhotonView.Find(id).gameObject;
    		o.transform.position = Vector3.Lerp (o.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth);
    		o.transform.rotation = Quaternion.identity;
    	}
    
    	[PunRPC]
    	void pickup(int viewID) {		
    			int x = Screen.width / 2;
    			int y = Screen.height / 2;
    
    			Ray ray = mainCamera.GetComponent().ScreenPointToRay(new Vector3(x,y));
    			RaycastHit hit;
    			if(Physics.Raycast(ray, out hit)) {
    				Pickupable p = hit.collider.GetComponent();
    				if(p != null && hit.distance < 5) {
    					carrying = true;
    					carriedObject = p.gameObject;
    					pView = carriedObject.GetComponent();
    					pView.viewID = viewID;						
    					p.gameObject.GetComponent().useGravity = false;					
    				}
    			}		
    	}
    	[PunRPC]
    	void checkDrop() {			
    		if(Input.GetKeyDown (KeyCode.E)) {
    			dropObject();
    		}
    	}
    	[PunRPC]
    	void dropObject() {
    		carrying = false;		
    		carriedObject.gameObject.GetComponent().useGravity = true;
    		carriedObject = null;
    	}
    	[PunRPC]
    	void throwObject(float pushForce){			
    		carrying = false;
    		if(carriedObject.tag != "BoxMultiplayer")
    			carriedObject.tag = "Projectile";
    		carriedObject.GetComponent ().AddForce(Camera.main.transform.forward * pushForce * 200);
    		carriedObject.gameObject.GetComponent ().useGravity = true;			
    		carriedObject = null;
    	}	
    
    }

    This code allows both players to grab and move around gameObjects, but now these objects are not visible for other players
  • GiViZedGiViZed
    edited November 2016
    So, someone told me that you are supposed to request ownership of an object each time you are going to move it, but how do i do it? Obviously i should not put RequestOwnership() in Update() function
Sign In or Register to comment.