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();
}
}