如何通过网络同步场景对象的刚体运动学?

问题描述

我使用 photon pun2 进行网络连接。这是一个简单的投掷场景,网络上的玩家试图接球和投球。 我的场景包含一个玩家可以选择和投掷的球。拾取物体时,我使用 punrpc 和 ipunobersvable 使球的运动学为真,而投掷运动学为假

跨网络同步刚体运动是一个问题。这样刚体运动学在关闭和打开之间切换。

我正在使用 photonrigidbodyview 进行网络同步。速度和角速度与变换完美同步。

这是代码

    private void Start()
    {          
           
            animator = GetComponent<Animator>();
            ball = GameObject.Find("IceBall").transform;
            rb = GameObject.Find("IceBall").GetComponent<Rigidbody>();
            coll = GameObject.Find("IceBall").GetComponent<SphereCollider>();
 
        
            //ballContainer = player.Find("ObjectGuide").transform;
            //fpsCamera = player.Find("CameraHolder").transform;
        
       
        //setup 
        if (!equipped)
        {
            rb.isKinematic = false;
            coll.isTrigger = false;
        }
        else
        {
            rb.isKinematic = true;
            coll.isTrigger = true;
            slotFull = true;
        }
 
    }
 
    private void Update()
    {
        Vector3 distanceToPlayer = ball.position - transform.position;
        if(!equipped && distanceToPlayer.magnitude<=pickupRange && Input.GetKeyDown(KeyCode.E)&&!slotFull)
        {
            photonView.RPC("PickUp",RpcTarget.AllBuffered,true);
        }
        if(equipped && Input.GetKey(KeyCode.Q))
        {
            photonView.RPC("Drop",false);
            
        }
    }
 
    [PunRPC]
    public void PickUp(bool _kinematic)
    {
        if(equipped == false && slotFull == false)
        {
            equipped = true;
            slotFull = true;
            Debug.Log("ball is equipped "+equipped);
            ball.GetComponent<PhotonView>().TransferOwnership(PhotonNetwork.LocalPlayer);
            rb.isKinematic = _kinematic;
 
            coll.isTrigger = true;
 
            //Make ball a child of the camera and move it to default position 
            if (photonView.IsMine)
            {   
                ball.parent = ballContainer;
                ball.localPosition = Vector3.zero;
                ball.localRotation = Quaternion.Euler(Vector3.zero);
            }
      
        }
 
 
    }
 
    [PunRPC]
    public void Drop(bool _kinematic)
    {
        if(equipped == true && slotFull == true)
        {
            ball.GetComponent<PhotonView>().TransferOwnership(null);
            animator.SetTrigger("IsThrowBall");
            equipped = false;
            slotFull = false;
 
            //set parent to null 
            ball.SetParent(null);
 
 
            //Make Rigidbody is not Kinematic and BoxCollider normal
            rb.isKinematic = _kinematic;
            coll.isTrigger = false;
 
            //Ball carries momentum of player 
           
            //addforce 
            rb.AddForce(fpsCamera.forward * dropForwardForce,ForceMode.Impulse);
            rb.AddForce(fpsCamera.up * dropUpwardForce,ForceMode.Impulse);
        }
       
 
    }
 
    public void OnPhotonSerializeView(PhotonStream stream,PhotonMessageInfo info)
    {
 
 
        if (stream.IsWriting)
        {
            // We own this player: send the others our data
            stream.SendNext(rb.isKinematic);
            stream.SendNext(coll.isTrigger);
        }
        else
        {
            // Network player,receive data
            this.rb.isKinematic = (bool)stream.ReceiveNext();
            this.coll.isTrigger = (bool)stream.ReceiveNext();
 
        }
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)