如何使脚本适用于所有玩家Unity Photon?

时间:2016-11-23 18:25:47

标签: c# unity3d unity5 photon

  

我希望所有玩家能够在场景中移动并投掷游戏对象   用这个脚本:

 [RequireComponent( typeof( PhotonView ) )]
 public class PickupObjectNetwork : Photon.MonoBehaviour {
 public GameObject mainCamera;    
 public bool carrying;
 GameObject carriedObject;
 public float distance;
 public float smooth;
 public float throwPower = 10f;
 private float chargeTime = 0f;    
 // Use this for initialization
 void Start () {
     mainCamera = GameObject.FindWithTag("MainCamera");            
 }
 // Update is called once per frame
 void Update () {
     if(photonView.isMine)
     {            
         if(carriedObject == null)
         {
             carrying = false;
         }
         if(gameObject == null)
             return;

         if(carrying) {
             photonView.RPC("carry", PhotonTargets.AllBuffered);
             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)) {                    
                 photonView.RPC("pickup", PhotonTargets.AllBuffered);
             }
         }
         if(Input.GetMouseButton(0)){
             chargeTime += Time.deltaTime;
         }
     }
 }
 [PunRPC]
 void carry() {            
     if (carriedObject == null)
     {
         return;
     }        
     Debug.Log("picked up object with id: " + carriedObject.GetComponent<PhotonView>().viewID);
     carriedObject.transform.position = Vector3.Lerp (carriedObject.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth);
     carriedObject.transform.rotation = Quaternion.identity;
 }
 [PunRPC]
 void pickup() {                
         int x = Screen.width / 2;
         int y = Screen.height / 2;
         Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3(x,y));
         RaycastHit hit;
         if(Physics.Raycast(ray, out hit)) {
             Pickupable p = hit.collider.GetComponent<Pickupable>();
             if(p != null && hit.distance < 5) {
                 carrying = true;
                 carriedObject = p.gameObject;                                
                 p.gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
                 p.gameObject.GetComponent<Rigidbody>().useGravity = false;                            
             }
         }        
 }
 [PunRPC]
 void checkDrop() {            
     if(Input.GetKeyDown (KeyCode.E)) {
         dropObject();
     }
 }
 [PunRPC]
 void dropObject() {        
     carrying = false;        
     carriedObject.gameObject.GetComponent<Rigidbody>().useGravity = true;
     carriedObject = null;
 }
 [PunRPC]
 void throwObject(float pushForce){            
     carrying = false;
     if(carriedObject.tag != "BoxMultiplayer")
         carriedObject.tag = "Projectile";
     carriedObject.GetComponent<Rigidbody> ().AddForce(Camera.main.transform.forward * pushForce * 200);
     carriedObject.gameObject.GetComponent<Rigidbody> ().useGravity = true;            
     carriedObject = null;
 }    
 }
  

我使用实例化场景对象   PhotonNetwork.InstantiateSceneObject();此脚本仅适用于   主客户端,非主客户端无法移动对象。

     

我也尝试过这个脚本(随机):http://pastebin.com/7DgjZ9U7   我真的不知道如何使用AllocateViewId()。当我用这个   脚本,两个玩家都可以移动sceneObjects,但对象移动不是   同步(对象位置不会更新为其他玩家)。   此外,当其中一个玩家抛出gameObject时   墙,用PhotonNetwork.Destroy()破坏新的碰撞,   这个玩家为别人冻结了。   希望我的问题不是超级凌乱。提前谢谢!

1 个答案:

答案 0 :(得分:1)

如果没有对象的权限的玩家处理它,则更改将不会同步到其他玩家。为了做到这一点,每个玩家需要在拾取/抛出/携带之前使用photonView.RequestOwnership()方法请求该对象的所有权。

还有一个关于如何在Photon Unity包上请求所有权的完整示例( DemoChangeOwner-Scene.unity )。