Search Unity

unity job system unit movement stutter

Discussion in 'C# Job System' started by tommox86, Jun 9, 2019.

  1. tommox86

    tommox86

    Joined:
    Apr 30, 2015
    Posts:
    88
    I am currently moving all my units in an rts style game with the job system. profiler shows 1000fps (profiler doesnt show job performance yet). The problem is the units are stuttering in their movement randomly, this happens with just 1 unit instantiated. The projectiles(slower projectiles such as rockets) also show this stutter. Is there a setting i need to set or am i missing something?

    unity version 2019.1.5f1 with latest preview packages, leak detection off
    development build
    burst is enabled
    example code: position.Value = position.Value+(-localToWorld,forward) * deltaTime*7;
    i have tried many variants of lerp same result. issue seems to be the job system

    it seems to run smoother in the editor than the actual build..

    on another note the profiler shows 0.5kb of garbage collection for 'DirtyTransformAccessArrayUpdate' but shows not spike in cpu(steady 1000fps) is this cause for alarm?
     
    Last edited: Jun 9, 2019
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Take a look in the profiler window. In particular Timeline view mode to see the jobs.
     
  3. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Show full code of your move system
     
  4. tommox86

    tommox86

    Joined:
    Apr 30, 2015
    Posts:
    88
    full code for movement, i have moved back to MoveTowards with same stutter


    Code (CSharp):
    1. using Unity.Burst;
    2. using Unity.Jobs;
    3. using UnityEngine;
    4. using UnityEngine.Jobs;
    5. using Unity.Mathematics;
    6. using Unity.Transforms;
    7. using Unity.Physics;
    8. using Unity.Entities;
    9. using Unity.Collections;
    10.  
    11.  
    12. public class unitMovementJob : JobComponentSystem
    13. {
    14.  
    15.  
    16.     [BurstCompile]
    17.     public struct RotationSpeedJob : IJobForEachWithEntity<Translation, Rotation, unitTag, parentData>
    18.     {
    19.         // public float speed;
    20.         public float topBound;
    21.         public float bottomBound;
    22.         public float deltaTime;
    23.  
    24.         public float time;
    25.  
    26.         [ReadOnly] public ComponentDataFromEntity<LocalToWorld> local;
    27.  
    28.  
    29.         public void Execute(Entity entity, int index, ref Translation position, ref Rotation rotation, ref unitTag unit, ref parentData pData)
    30.         {
    31.  
    32.          
    33.             if ( pData.set)
    34.             {
    35.  
    36.                 position.Value = Vector3.MoveTowards(position.Value, pData.destination, pData.unitSpeed * deltaTime);
    37.  
    38.                 if (!(position.Value - pData.destination).Equals(float3.zero))
    39.                 {
    40.                     unit.rot = Quaternion.LookRotation(position.Value - pData.destination);
    41.                     unit.rot.x = 0.0f;
    42.                     unit.rot.z = 0.0f;
    43.                
    44.                     rotation.Value = Quaternion.Slerp(rotation.Value, unit.rot, deltaTime * 15f);
    45.                 }
    46.  
    47.             }
    48.        
    49.             if (!pData.destination.Equals(float3.zero) && Vector3.Distance(position.Value, pData.destination) < 5f)
    50.             {
    51.  
    52.                 pData.destination = Vector3.zero;
    53.                 pData.set = false;
    54.             }
    55.          
    56.  
    57.             unit.position = position.Value;//value;
    58.             unit.rot = rotation.Value;
    59.             pData.position = position.Value;
    60.  
    61.         }
    62.     }
    63.  
    64.  
    65.     protected override JobHandle OnUpdate(JobHandle inputDependencies)
    66.     {
    67.  
    68.         var job = new RotationSpeedJob()
    69.         {
    70.             local = GetComponentDataFromEntity<LocalToWorld>(),
    71.             deltaTime = Time.deltaTime,
    72.             time = Time.time,
    73.         };
    74.    
    75.         return job.Schedule(this, inputDependencies);
    76.  
    77.    
    78.  
    79.     }
    80. }
     
    Last edited: Jun 9, 2019
  5. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Oh god...use code snippet for forum
     
  6. tommox86

    tommox86

    Joined:
    Apr 30, 2015
    Posts:
    88
    fixed do you see anything?
     
  7. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Replace quaternion and vector3 methods to another from Mathematics library, they are optimized for SIMD.
     
  8. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    About stutter. It can happen, your units going through target position rotates and going back (cos delta with speed can be greater than distance to target). Try add check if distance to target less than your movement delta clamp your movement delta by current distance.
     
  9. tommox86

    tommox86

    Joined:
    Apr 30, 2015
    Posts:
    88
  10. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
  11. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    492
    I used to have movement stutter with ECS + jobs + burst + CameraFollow. It was alleviated by turning on incremental garbage collection.

    Now that was for the player build. For the Editor, I've always had stutter since Unity 5. Tried everything like everyone else, but just learned to ignore it. Incremental garbage collector is enabled for the Editor in 2019.2, but it doesn't seem to help that much. Game view and Scene view UI code just keep spiking randomly.

    For your case, I think you should first see if it's really the Job system by turning off jobs and do things on the mainthread for comparison.
     
  12. tommox86

    tommox86

    Joined:
    Apr 30, 2015
    Posts:
    88
    i have finally traced the jitter cause. This is copy transform to gameobject proxy script. I have a gameobject which contains a status canvas. The status holds the vehicle health status bar and is instantiated at runtime with the following mono's:
    'copy transform to gameobject proxy' : 'game object entity' and 'convert to entity with convert and inject gameobject' when i remove the 'copy transform to gameobject proxy' script the major jitters are no more. linked entity for this status is moved by the job system. Any other way of displaying such a system without using this transform to game object proxy ?

    note that jitters are there even when the gameobject is instantiated and not being moved by any job system..

    An empty gameobject with the above components is instantiated, my canvas system is then instantiated and parented to the gameobject, else the conversion system throws an error
     
  13. tommox86

    tommox86

    Joined:
    Apr 30, 2015
    Posts:
    88
    it seems any type of entity conversion of a gameobject with child will cause this issue.
    to replicate, create camera with rotation 90 on the x axis, place a cube under it, attach this script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NewBehaviourScript : MonoBehaviour
    5. {
    6.  
    7.     private void Start()
    8.     {
    9.         Application.targetFrameRate = 60;
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.         if (Input.GetKey(KeyCode.UpArrow))
    16.         {
    17.             transform.position += Vector3.forward * Time.deltaTime * 25f;
    18.         }
    19.         if (Input.GetKey(KeyCode.DownArrow))
    20.         {
    21.             transform.position -= Vector3.forward * Time.deltaTime * 25f;
    22.         }
    23.         if (Input.GetKey(KeyCode.RightArrow))
    24.         {
    25.             transform.position -= Vector3.left * Time.deltaTime * 25f;
    26.         }
    27.         if (Input.GetKey(KeyCode.LeftArrow))
    28.         {
    29.             transform.position -= Vector3.right * Time.deltaTime * 25f;
    30.         }
    31.     }
    32. }
    33.  
    build and see how smooth it is. now add another cube to the scene with a child cube. attach conver to entity(destroy) and see how jerky the movement is. (even though this is mono update, its aparrant in ecs jobs and non jobs).
     
  14. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Now it's more clear. You're about camera rendering stuttering, cos from you previous posts it's absolutely unclear. Yep now we need some one from Unity :)
     
  15. tommox86

    tommox86

    Joined:
    Apr 30, 2015
    Posts:
    88
    So it def seems like an internal bug.
    When i remove the Hybrid Renderer package and instantiate the entity, the stutter completely disappears(the entity isnt rendered but is instantiated)
     
    Last edited: Jun 12, 2019
  16. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Yes it’s renderer problem seems order of updating renderer and transforms. @Joachim_Ante I also can reproduce it
     
    Krajca, tarahugger and tommox86 like this.
  17. tommox86

    tommox86

    Joined:
    Apr 30, 2015
    Posts:
    88
    Still an issue with the latest update
     
    S_Darkwell and RaL like this.
  18. Zorkind

    Zorkind

    Joined:
    May 4, 2015
    Posts:
    55
    I have this with even 2018.2 Unity -_- so annoying.
     
  19. shamsfk

    shamsfk

    Joined:
    Nov 21, 2014
    Posts:
    307
    I'm experiencing it now in 2020.3.30. A lot of fps 450 if not limited, simplest movement code possible:
    Code (CSharp):
    1.  Entities.ForEach((
    2.                 ref Translation translation,
    3.                 ref ActorMoveData movement) =>
    4.             {
    5.                 translation.Value += movement.Direction * movement.Speed * deltaTime;
    6.             }).ScheduleParallel();
    and it stutters, 2 days I fought the thing, and found this post, but with no answer