2

I'm developing a multiple fps game with Unity using Photon PUN2 and I get a problem with using ragdolls sync player positions on the network player position moving random on every client how can I fix it

I enable ragdoll with this code:

 public PhotonView pv;

    public Collider headCollider;

    public denemeRotate dr;

    // Start is called before the first frame update
    void Start()
    {
        gameObject.AddComponent<SyncRagdoll>();
        gameObject.AddComponent<PhotonRigidbodyView>();
        gameObject.AddComponent<PhotonTransformView>();
        pv.RPC("setRigidbodyState",RpcTarget.All,true);
        pv.RPC("setColliderState", RpcTarget.All,false);
    }

    // Update is called once per frame
    void Update()
    {
        
    }


    [PunRPC]
    public void Die(Vector3 hitNormal,int currentW)
    {
        Destroy(dr);
        GetComponent<Animator>().enabled = false;
        pv.RPC("setRigidbodyState", RpcTarget.All, false);
        Rigidbody[] rigidbodies = GetComponentsInChildren<Rigidbody>();

        foreach (Rigidbody rigidbody in rigidbodies)
        {     
            if(currentW == 5)
            {
                rigidbody.AddForce(-hitNormal * 200f, ForceMode.Impulse);
            }
            else
            {
                rigidbody.AddForce(-hitNormal * 100f, ForceMode.Impulse);
            }      
        }
        pv.RPC("setColliderState", RpcTarget.All, true);
    }

    [PunRPC]
    void setRigidbodyState(bool state)
    {
        Rigidbody[] rigidbodies = GetComponentsInChildren<Rigidbody>();

        foreach (Rigidbody rigidbody in rigidbodies)
        {
            rigidbody.isKinematic = state;
        }

     
    }

    [PunRPC]
    void setColliderState(bool state)
    {
        Collider[] colliders = GetComponentsInChildren<Collider>();

        foreach (Collider collider in colliders)
        {
            if(collider != headCollider)
            {
                collider.enabled = state;
            }
         
        }

        

    }

I try this code to sync ragdolls but didn't work

 public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.IsWriting)
        {
            stream.SendNext(transform.position);
            stream.SendNext(transform.rotation);
            stream.SendNext(GetComponent<Rigidbody>().velocity);
            stream.SendNext(GetComponent<Rigidbody>().angularVelocity);
            stream.SendNext(GetComponent<Rigidbody>().drag);
            stream.SendNext(GetComponent<Rigidbody>().angularDrag);
            stream.SendNext(GetComponent<Rigidbody>().mass);
        }
        else
        {
            Vector3 syncPosition = (Vector3)stream.ReceiveNext();
            Quaternion syncRotation = (Quaternion)stream.ReceiveNext();
            Vector3 syncVelocity = (Vector3)stream.ReceiveNext();
            Vector3 syncAngularVelocity = (Vector3)stream.ReceiveNext();

            transform.position = syncPosition;
            transform.rotation = syncRotation;
            GetComponent<Rigidbody>().velocity = syncVelocity;
            GetComponent<Rigidbody>().angularVelocity = syncAngularVelocity;
            GetComponent<Rigidbody>().drag = (float)stream.ReceiveNext();
            GetComponent<Rigidbody>().angularDrag = (float)stream.ReceiveNext();
            GetComponent<Rigidbody>().mass = (float)stream.ReceiveNext();
        }
    }
Ken White
  • 123,280
  • 14
  • 225
  • 444
TanerSHN
  • 31
  • 2

1 Answers1

1

create a boolean Activate Ragdoll, and share its state via stream as you are doing. When receiving, check the IF on Update, if it is true, then disable the Animator Component

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 08 '23 at 19:38
  • thanks,i did but im not trying to sync ragdoll enable or disable im trying to sync ragdoll physics – TanerSHN May 09 '23 at 03:15