Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

[Resolved] Movement not predicted - NetCode

Discussion in 'NetCode for ECS' started by TomTales, Feb 5, 2020.

  1. TomTales

    TomTales

    Joined:
    Oct 8, 2019
    Posts:
    8
    I'm working from the multiplayerSample and I tried changing the movement, but now it isn't predicted anymore for the client.

    I replaced the commented part with the part underneath.
    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.NetCode;
    3. using Unity.Transforms;
    4. using UnityEngine;
    5.  
    6. #if true
    7.  
    8. [UpdateInGroup(typeof(GhostPredictionSystemGroup))]
    9. public class MoveCubeSystem : ComponentSystem
    10. {
    11.     protected override void OnUpdate()
    12.     {
    13.         var group = World.GetExistingSystem<GhostPredictionSystemGroup>();
    14.         var tick = group.PredictingTick;
    15.         var deltaTime = Time.DeltaTime;
    16.         Entities.ForEach((DynamicBuffer<CubeInput> inputBuffer, ref Translation trans, ref MoveVelocity moveVelocity, ref PredictedGhostComponent prediction) =>
    17.         {  
    18.             if (!GhostPredictionSystemGroup.ShouldPredict(tick, prediction))
    19.                 return;
    20.             CubeInput input;
    21.             inputBuffer.GetDataAtTick(tick, out input);
    22.             //if (input.horizontal > 0)
    23.             //    trans.Value.x += deltaTime;
    24.             //if (input.horizontal < 0)
    25.             //    trans.Value.x -= deltaTime;
    26.             //if (input.vertical > 0)
    27.             //    trans.Value.z += deltaTime;
    28.             //if (input.vertical < 0)
    29.             //    trans.Value.z -= deltaTime;
    30.  
    31.             float runAcceleration = 1;
    32.             float maxRunSpeed = 10;
    33.  
    34.             Vector3 inputs = new Vector3(input.horizontal, 0, input.vertical);
    35.             if (inputs.magnitude > 1)
    36.                 inputs = inputs.normalized;
    37.             moveVelocity.Value += inputs * runAcceleration;
    38.  
    39.             float runningDragCoeff = runAcceleration / (maxRunSpeed + runAcceleration);
    40.             Vector3 runningDrag = new Vector3(moveVelocity.Value.x, 0, moveVelocity.Value.z) * -runningDragCoeff;
    41.             moveVelocity.Value += runningDrag;
    42.  
    43.             trans.Value.x += moveVelocity.Value.x * deltaTime;
    44.             trans.Value.z += moveVelocity.Value.z * deltaTime;
    45.         });
    46.     }
    47. }
    48.  
    49. #endif
    50.  
    I also created a component containing the velocity:
    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.NetCode;
    3. using UnityEngine;
    4.  
    5. public struct MoveVelocity : IComponentData
    6. {
    7.     public Vector3 Value;
    8. }
    9.  
    And added that component to the cube in the game.cs file:
    Code (CSharp):
    1. var ghostCollection = GetSingleton<GhostPrefabCollectionComponent>();
    2.             var ghostId = MehGhostSerializerCollection.FindGhostType<PlayerSnapshotData>();
    3.             var prefab = EntityManager.GetBuffer<GhostPrefabBuffer>(ghostCollection.serverPrefabs)[ghostId].Value;
    4.             var player = EntityManager.Instantiate(prefab);
    5.             EntityManager.SetComponentData(player, new MovableCubeComponent { PlayerId = EntityManager.GetComponentData<NetworkIdComponent>(reqSrc.SourceConnection).Value });
    6.             EntityManager.AddComponentData<MoveVelocity>(player, new MoveVelocity());
    7.  
    8.             PostUpdateCommands.AddBuffer<CubeInput>(player);
    9.             PostUpdateCommands.SetComponent(reqSrc.SourceConnection, new CommandTargetComponent { targetEntity = player });
    If I uncomment the first original part and comment my added code, the movement is predicted for the client.
    Do I have to send the velocity as ghost as well? Any help is appreciated! :)
     
  2. SebLazyWizard

    SebLazyWizard

    Joined:
    Jun 15, 2018
    Posts:
    234
    Since the system in which you added that component to the player ghost runs on the server, only the server-side ghost has that component, so your move system also only runs on the server ofc.
     
  3. TomTales

    TomTales

    Joined:
    Oct 8, 2019
    Posts:
    8
    So that's the "EntityManager.AddComponentData<Movevelocity>(player, new MoveVelocity);" line in the game.cs file?
    Where would I add it to solve the issue? Thanks for the answer!
     
  4. SebLazyWizard

    SebLazyWizard

    Joined:
    Jun 15, 2018
    Posts:
    234
    The easiest way is to just add that component to the ghost prefab itself, but dont make it sync (so no GhostDefaultField attribute).
    If you dont want to go for that, you also have to add the component to the system (SampleCubeInput) where the local client ghost get's set up;
    Code (CSharp):
    1.                 if (cube.PlayerId == localPlayerId)
    2.                 {
    3.                     PostUpdateCommands.AddBuffer<CubeInput>(ent);
    4.                     PostUpdateCommands.SetComponent(GetSingletonEntity<CommandTargetComponent>(), new CommandTargetComponent {targetEntity = ent});
    5. //add the component here
    6.                 }
    Also make your compent data a float3, not a Vector.
     
  5. TomTales

    TomTales

    Joined:
    Oct 8, 2019
    Posts:
    8
    Cool! I think I understand it a bit more now. I was wondering about the different ghosts before but didn't quite understand. I changed the component data to float3. I wanted to before but didn't quite understand how to get the magnitude of float3's, but I've changed that now.

    It does add a new problem tho. The movement rubberbands a lot now. It predicts the movement client-side then kind of goes back to where it was and then gets the server-side information and moves back to where it should be.
    How come it isn't syncing correctly?
    Thanks a lot for your help SebLazyWizard. I haven't really used forums before and you're a great help!
    I will, of course, try to solve it myself ;)
     
  6. SebLazyWizard

    SebLazyWizard

    Joined:
    Jun 15, 2018
    Posts:
    234
    Debug the final translation data in the system and check for differences between server and client. Are you applying some latency to the simulation? If yes then that's likely caused by that, cause there's still a bug related to it.
     
  7. timjohansson

    timjohansson

    Unity Technologies

    Joined:
    Jul 13, 2016
    Posts:
    473
    It looks like you are updating the MoveVelocity component in the prediction group, when doing that you have to make sure the velocity is synced or you will get rubberbanding.
    The way prediction works it to restore the server state to all replicated fields, then run the simulation from the restored tick to the current server tick. If you update a non-synced field in the prediction group you will update it multiple times every frame which will result in the client moving way to fast and then snapping back to the servers position.
     
  8. TomTales

    TomTales

    Joined:
    Oct 8, 2019
    Posts:
    8
    Thank you Tim Johansson and SebLazyWizard! The movement is correctly predicted now. All I had to do was add a field for the velocity value, with quantization and interpolation, on the ghost authoring script. I have the occasional jitter when stressing it with latency above 100ms, but for the rest it works perfectly.

    So to recap for myself:
    -I need to send the inputs from client to server.
    -Add the moveVelocity component, with [GenerateAuthoringComponent] and [GhostDefaultValue], to the prefab. This way it get's added to the server- and client-ghost.
    -Make sure it actually has a field with quantization and interpolation so that the value syncs between server and client.
    -Use the moveVelocity to move the player in the MoveCubeSystem.

    Is that all correct? Many thanks!
     
  9. SebLazyWizard

    SebLazyWizard

    Joined:
    Jun 15, 2018
    Posts:
    234
    If you don't need the velocity component to be synced on interpolated clients (which is the case from just the code you posted), then only send it to predicted ghosts, that saves a bit bandwidth.
     
  10. TomTales

    TomTales

    Joined:
    Oct 8, 2019
    Posts:
    8
    Yeah, ok. I can't just sync everything on the player. Thank you!

    I thought I wouldn't have to sync the velocity since it would only change through the input, but it has the desired effect when synced. I think it would also make more sense to be synced if the velocity were to change from something else than the input, like another player bumping you or something.