Search code examples
c#unity-game-enginephoton

Change in player speed causes a jerky movement in unity photon fusion multiplayer


I am working on a 2D multiplayer game in unity with photon fusion. My player has a default player speed say 5f. I am changing the player speed (to 10f) when I want the player to move faster for a specific duration. Later which I want my player to move with default speed. The change in speed gives a jerky movement especially on the client. The jerkiness happens when the speed is changed from default to 10f, the movement with speed 10f is fine for the specific duration and again while changing the speed from 10f to default gives a jerkiness. I believe the sudden drastic change in speed is being mis-predicted and correcting this predicted value is the reason for the jerky movement.

The below is part of my code:

using UnityEngine;
using Fusion;

public class Player : NetworkBehaviour
{
    [Networked] public float PlayerSpeed { get; set; }

    public override void Spawned()
    {
        RPC_SetPlayerSpeed(5f);
    }

    public override void FixedUpdateNetwork()
    {
        // below is psuedo code
        if (LShiftPressed)
        {
            RPC_SetPlayerSpeed(10f);
        }

        if (TimerIsDone)
        {
            RPC_SetPlayerSpeed(5f);
        }
    }


    [Rpc(RpcSources.All, RpcTargets.All)]
    public void RPC_SetPlayerSpeed(float speed)
    {
        PlayerSpeed = speed;
    }
}

PlayerRigidbody.MovePosition(PlayerRigidbody.position + movement * Runner.DeltaTime); - this is how I am moving my player.

Over the client side, input authority will call the RPC_SetPlayerSpeed to change the speed across the network.

I tried interpolating the NetworkRigidbody2d component to all the available interpolation data sources(Auto, snapshots, predicted, no interpolation) in photon fusion. What am I missing?


Solution

  • To achieve smooth movement of an object, it is generally not possible to directly change its velocity, but rather to use interpolation (Lerp) method.