Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Client side projectile prediction lag compensation

Discussion in 'NetCode for ECS' started by earlgeorg, Jul 2, 2023.

  1. earlgeorg

    earlgeorg

    Joined:
    Jan 21, 2015
    Posts:
    5
    Hi,

    I am stuck at correctly updating the projectile I am shooting from my player.
    I am updating it every OnUpdate:

    Code (CSharp):
    1.     [UpdateInGroup(typeof(PredictedSimulationSystemGroup))]
    2.     public partial struct NetworkProjectileSystem : ISystem
    3.     {
    4.         public void OnUpdate(ref SystemState state)
    5.         {
    6.             foreach (var (data, transform, entity) in SystemAPI.Query<NetworkProjectileData, LocalTransform>()
    7.                          .WithEntityAccess().WithAll<Simulate>())
    8.             {
    9.                 var newTransform = transform;
    10.                 var newData = data;
    11.  
    12.                 var from = transform.Position;
    13.                 var distance = data.stats.velocity * deltaTime;
    14.                 newTransform.Position += newTransform.Forward() * distance;
    15.                 ecb.SetComponent(entity, newTransform);
    16.             }
    17.             ecb.Playback(...)
    18.         }
    19.     }
    However when I log out the data, only the server successfully updates the values of the projectile.
    [Client] [124] [Entity(428:2)] Pos: 2.631364 -> 2.763488
    [Client] [125] [Entity(428:2)] Pos: 2.631364 -> 2.763488
    [Client] [126] [Entity(428:2)] Pos: 2.631364 -> 2.659181

    As you can see I am updating the positions in the loop, but in every prediction step the values are reset to their original value again.

    The projectile prefab is set to OwnerPredicted, with HasOwner flag enabled.
    The projectile gets instantiated in the client & server OnUpdate and the owner is correctly set.

    Do you have any idea what I am missing to get this working?

    I am working on the latest ECS 1.0
     
  2. frimarichard

    frimarichard

    Joined:
    Jul 24, 2017
    Posts:
    31
    I'm seeing this exact same problem with a laggy client (~1s) setting their LocalTransform position. Within the predication loop, the client sets their position, but then in the next loop with the next serverTick, it's back to using the server's value. I don't see how the predication can work if the server always stomps the value, even though the client is far behind the server's tick.
     
  3. frimarichard

    frimarichard

    Joined:
    Jul 24, 2017
    Posts:
    31
    Nevermind, I only see this pattern when using the simulator in the multiplayer tools. With a natural lag, all is working as expected.