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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…