Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

unity3d - How to sync scene object's rigidbody (kinematic) over the network?

I am using photon pun2 for networking. it's a simple pick and throw scene where players on network trying to pick and throw the ball. My scene contains a ball that players can pick and throw. When picking the object I am making ball's kinematic true and for throw kinematic is false using punrpc and ipunobersvable

syncing rigidbody kinematic across the network is an issue. this way rigidbody kinematic is switching between off and on.

I am using photonrigidbodyview for syncing over the network. velocity and angular velocity with transform is perfectly synced.

Here is the code

    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", RpcTarget.AllBuffered, 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();
 
        }
    }
question from:https://stackoverflow.com/questions/65932806/how-to-sync-scene-objects-rigidbody-kinematic-over-the-network

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...