How to smoothen out ball movement over network

Hi, I am making an online 2D soccer game using Photon and need help getting the ball's movement to be smoother. I have already set up the player movement but when the ball moves it is very glitchy and jumps around a lot. I have the following code on the ball:

public class ballMoveNetworking : MonoBehaviour {
Vector3 targetPos;

PhotonView PhotonView;

// Use this for initialization
void Start () {
PhotonView = GetComponent ();

}

// Update is called once per frame
void Update () {
if (!PhotonView.isMine) {
smoothMove ();
}
}


private void smoothMove(){
transform.position = Vector3.Lerp(transform.position, targetPos, 0.25f );
}



private void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
if (stream.isWriting) {
stream.SendNext (transform.position);

} else {
targetPos = (Vector3)stream.ReceiveNext ();

}
}
}


This script is in the 'Observed Components' option in a Photon View Component on the ball.
Thanks in advance.

Best Answer

  • [Deleted User]
    Answer ✓
    Hi @Samuel,

    you have some delay between sending the position data on one side and receiving it on the other side. This might result in the glitches you have mentioned. If the object has a Rigidbody component which you also use, things might get worse. To reduce the amount of lag and see if this is already enough to solve the problem I would recommend you taking a look at the Lag Compensation documentation page. It covers most use cases with different approaches about how to get better synchronization results.

Answers

  • [Deleted User]
    Answer ✓
    Hi @Samuel,

    you have some delay between sending the position data on one side and receiving it on the other side. This might result in the glitches you have mentioned. If the object has a Rigidbody component which you also use, things might get worse. To reduce the amount of lag and see if this is already enough to solve the problem I would recommend you taking a look at the Lag Compensation documentation page. It covers most use cases with different approaches about how to get better synchronization results.
  • Thanks it worked