Letsgo66
Letsgo66

Reputation: 11

How to fix multiplayer stuttering?

For some reason the other player's movement is 'stuttering'. I know this is a common problem people run into with Photon, so was wondering if anyone knows how I can resolve it?

Here's my player movement code:

public float SmoothingDelay = 5;

public void Start() 
{
    GetComponent<SmoothSyncMovement>().enabled = true; //This is the name of this script
}

public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
    if (stream.isWriting)
    {
        //We own this player: send the others our data
        stream.SendNext(rb2D.position);
        stream.SendNext(rb2D.rotation); 
    }
    else
    {
        //Network player, receive data
        correctPlayerPos = (Vector3)stream.ReceiveNext();

    }
}

public void Update()
{
    if (!photonView.isMine)
    {
        Vector2 playerMovement = rb2D.position + velocity * Time.deltaTime;
        rb2D.MovePosition(playerMovement);          
    }

    if (photonView.isMine)
    {
        Vector2 playerMovement = rb2D.position + velocity * Time.deltaTime;      
        rb2D.MovePosition(playerMovement);
    }
}

Upvotes: 1

Views: 1323

Answers (1)

Anthony Raimondo
Anthony Raimondo

Reputation: 1721

Make a smooth transition between players old possible and new position, dont set position. You can use lerp for this or any other method.

This video tutorial goes into all the details of photon networking including how to deal with stuttering and other issues you might run into. Video Tutorial Play List

Exact Video That Addresses Stuttering

It would look something like this:

currentPosition = Vector3.Lerp(currentPosition, realPosition,0.1f);

Where currentPosition is the position of the networked player before it moves on your client and realPosition is the new position received from the network where you want the player to be.

Upvotes: 1

Related Questions