- Home /
Question by
Krzysiek1105 · Jul 10, 2014 at 09:58 AM ·
rigidbodynetworkballpunsynchronize
[PUN] Rigidbody ball synchronization
Hi. I want to synchronize ball between players. This is my code, but this solution is not working good. I no have idea how to solve this problem.
using UnityEngine;
using System.Collections;
public class BallSynchronize : Photon.MonoBehaviour
{
Vector3 realPosition = Vector3.zero;
Quaternion realRotation = Quaternion.identity;
Vector3 realPositionR = Vector3.zero;
Vector3 realVelocityR = Vector3.zero;
Quaternion realRotationR = Quaternion.identity;
Vector3 realVelocityRA = Vector3.zero;
public void Update ()
{
if(!photonView.isMine)
{
transform.position = Vector3.Lerp(transform.position, realPosition, 0.1f);
transform.rotation = Quaternion.Lerp(transform.rotation, realRotation, 0.1f);
}
}
public void FixedUpdate ()
{
if(!photonView.isMine)
{
rigidbody.position = Vector3.Lerp(rigidbody.position, realPositionR, 0.1f);
rigidbody.velocity = Vector3.Lerp(rigidbody.velocity, realVelocityR, 0.1f);
rigidbody.rotation = Quaternion.Lerp(rigidbody.rotation, realRotationR, 0.1f);
rigidbody.angularVelocity = Vector3.Lerp(rigidbody.angularVelocity, realVelocityRA, 0.1f);
}
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if(stream.isWriting)
{
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
stream.SendNext(rigidbody.position);
stream.SendNext(rigidbody.rotation);
stream.SendNext(rigidbody.velocity);
stream.SendNext(rigidbody.angularVelocity);
}
else
{
realPosition = (Vector3)stream.ReceiveNext();
realRotation = (Quaternion)stream.ReceiveNext();
realPositionR = (Vector3)stream.ReceiveNext();
realRotationR = (Quaternion)stream.ReceiveNext();
realVelocityR = (Vector3)stream.ReceiveNext();
realVelocityRA = (Vector3)stream.ReceiveNext();
}
}
}
Comment
The ball is lagging (is not network connection problem), doesn't rotate.
Is the ball being instanciated? Because only the creator of it and the host can effect it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613153705im_/https://answers.unity.com/themes/thub/images/avi.jpg)