Weird 2D synchronization of position

I'm programming the player movement for a simple bomberman game.

Client-side movement is accurate. However, my position for other players in the game is not accurate (no, this certainly isn't lag).

If I'm standing in the very center of the map, my position gets synced correctly for other players over the network. However, if I move in any direction, my position for other players is always slightly off in that particular direction relative to the center.

If I move to the right, other players will see my player slightly more to the right of my actual position. The greater the distance to the center is (moving to the right in this case) the greater the difference between my actual position and the position of my player other players see.

Here's an attempt to illustrate my issue: https://www.imageupload.co.uk/image/EOM9

This is my PlayerMovement code:
public class PlayerMovement : Photon.MonoBehaviour {

private PhotonView PhotonView;
private Vector3 TargetPosition;

[SerializeField]
private float speed; // Set the value in the inspector in the NewPlayer prefab

private void Awake() {
PhotonView = GetComponent();
}

private void FixedUpdate() {
if(PhotonView.isMine) {
MovePlayer();
} else {
SmoothMove();
}
}

private void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) {
if(stream.isWriting) {
stream.SendNext(transform.position);
} else {
TargetPosition = (Vector3)stream.ReceiveNext();
}
}

private void MovePlayer() {
float vertical = Input.GetAxisRaw("Vertical"); // x-Achse
float horizontal = Input.GetAxisRaw("Horizontal"); // y-Achse

transform.position += transform.up * (vertical * speed * Time.fixedDeltaTime);
transform.position += transform.right * (horizontal * speed * Time.fixedDeltaTime);
}

private void SmoothMove() {
transform.position = Vector3.Lerp(transform.position, TargetPosition, Time.fixedDeltaTime * 8);
}

}

Comments

  • Hi @FBI,

    If I move to the right, other players will see my player slightly more to the right of my actual position.


    This is interesting because in other cases it is always the other way around. Have you already tried using the Update function instead of the FixedUpdate function? Another option you can check out is to use the Vector3.MoveTowards function instead of the Vector3.Lerp function. Using either one of these or both might end in a different result.
Sign In or Register to comment.