Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Entities not moving smoothly in DOTS frame by frame?

Discussion in 'Entity Component System' started by silentpundit, Oct 25, 2020.

  1. silentpundit

    silentpundit

    Joined:
    May 2, 2020
    Posts:
    26
    Hello all!

    I've got a project that is already fully created using gameobjects/monobehaviours, and I'm attempting to convert the traffic system to ECS.

    So as Baby's First Step, I've got a very bog-standard system in place for moving entities in a straight line. Here's the OnUpdate that makes it happen:

    Code (CSharp):
    1.  
    2. protected override void OnUpdate()
    3.     {
    4.         float deltaTime = Time.DeltaTime;
    5.  
    6.         Entities.
    7.             WithAll<TrafficCarTag>().
    8.             ForEach((ref Translation pos, in TrafficCarData trafficCarData, in Rotation rot) =>
    9.             {
    10.                 float3 forwardDirection = math.forward(rot.Value);
    11.                 pos.Value += forwardDirection * trafficCarData.speed * deltaTime;
    12.  
    13.  
    14.      
    15.             }).ScheduleParallel();
    16.     }
    17.  
    This appears to work, but...at a speed of 50, as I keep pace with these entities using the player car which is a GameObject (using RigidBody thrust to move, if that matters), I can see that the entities are very jerky as they move. On average they are moving at the right speed, and framerate is running smoothly--the entities themselves are stuttering in their movement.

    I can only assume the issue is with DeltaTime not being re-evaluated for each entity from frame to frame--it's being cached once per frame and that value is then handed off to the job system to apply it to each entity on more or less a random order. I have no idea if this is the actual cause, but it would seem to make sense, creating a slight jitter from frame to frame that's exacerbated at higher speeds.

    Has anyone run into this before?
     
    Last edited: Oct 25, 2020
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,217
    deltaTime wouldn't be reevaluated for each GameObject every frame either. You probably have some issue related to Update vs FixedUpdate and interpolation settings. Try having both entities and GameObjects drive side-by-side implementing identical logic.
     
  3. silentpundit

    silentpundit

    Joined:
    May 2, 2020
    Posts:
    26
    Thanks for the response. Yeah just double-checked by having entities and gameobjects driving side by side, both doing 'speed * deltaTime' in their respective ways. The gameobject is updating in FixedUpdate, and the entity is updating in SystemBase.OnUpdate and then scheduling the job in parallel. The game object is smoothly moving ahead, while the entity is stuttering dramatically.
     
  4. silentpundit

    silentpundit

    Joined:
    May 2, 2020
    Posts:
    26
    So just to see if it would work, I tried adding a
    [DisableAutoCreation] 
    to TrafficSystem (the system that moves the entities), and changed the deltaTime lookup to
    UnityEngine.Time.fixedDeltaTime
    . Then added a
    World.DefaultGameObjectInjectionWorld.GetOrCreateSystem<TrafficSystem>().Update();
    on a MonoBehaviour's FixedUpdate(). As a result the MonoBehaviour's FixedUpdate function is driving the system's Update().

    This fixed the stutter.

    I assume though that this is awful and will cause issues, buuuut....what works works! It also confirms you were right about the cause, @DreamingImLatios .

    EDIT - Further experimentation shows that undoing all the above changes except the change to UnityEngine.Time.fixedDeltaTime also (apparently) fixes the stutter but causes the cars to travel at approximately speed 7 instead of 50.
     
    Last edited: Oct 25, 2020
  5. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,217
    I suspect you may be missing interpolation on all vehicles. But I don't know enough about your project to say with certainty.

    As for the technique you used, while it may not be the best and some of the editor tools will break (they're pretty fragile to begin with), it actually shouldn't cause any real runtime problems. The ECS is designed such that you can pretty much run systems anywhere in the main thread. ScriptBehaviourUpdateOrder even has some methods to install systems directly into the player loop (which can give you more deterministic ordering of your ECS systems and FixedUpdate calls).
     
  6. silentpundit

    silentpundit

    Joined:
    May 2, 2020
    Posts:
    26
    How would I ascertain this, if you don't mind my asking? I'd be happy to share any details that'd be useful.
     
  7. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,217
    Change your FixedUpdate time to something like 0.2 or 0.4 instead of 0.02 and see if the motion starts to look really low framerate.
     
  8. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    I have also noticed occasional juttery movement when translating entities in DOTS.

    The hiccups I’ve seen are often quite subtle, and can only happen once every two or three seconds. After staring at my screen for ages, I wondered if I was losing my mind.

    I didn’t think to compare it to a GameObject moving alongside. Thanks for testing this and identifying an actual problem.