Search Unity

Resolved Unexpected different result setting Camera position with/without Job.Run

Discussion in 'Entity Component System' started by No3371, Oct 20, 2020.

  1. No3371

    No3371

    Joined:
    Dec 1, 2016
    Posts:
    42
    When revising a piece of code from my colleage, I noticed something weird. Following are 2 implementations of following camera, both works on Main thread, basically just take Translation of tracking entity then modify the position of the camera.

    This implementation cause camera following jitter.
    Code (CSharp):
    1.     public class CameraFollowSystem : SystemBase
    2.     {
    3.         internal Vector3 cameraOffset = new Vector3(0, 7.5f, -9f);
    4.         internal Quaternion cameraRot = Quaternion.Euler(45, 0, 0);
    5.         private float cameraSmoothSpeed = 5f;
    6.         private bool needInit = true;
    7.         public Camera Camera { get; internal set; }
    8.         PlayerViewSystem playerViewSystem;
    9.         protected override void OnUpdate()
    10.         {
    11.             if (playerViewSystem == null) playerViewSystem = World.GetExistingSystem<PlayerViewSystem>();
    12.             if (Camera == null || playerViewSystem == null || playerViewSystem.self == Entity.Null) return;
    13.             Camera.transform.position = (Vector3) EntityManager.GetComponentData<Translation>(playerViewSystem.self).Value + cameraOffset;
    14.         }
    15.     }
    This implementation cause no jitter at all.
    Code (CSharp):
    1. public class CameraFollowSystem : SystemBase
    2. {
    3.     private Vector3 posOffset = new Vector3(0, 7.5f, -9f);
    4.     private Quaternion angleOffset = Quaternion.Euler(45, 0, 0);
    5.     private float cameraSmoothSpeed = 5f;
    6.     private bool needInit = true;
    7.  
    8.     protected override void OnUpdate()
    9.     {
    10.         Entities.WithoutBurst().WithAll<Player.Component, ClientMovementInput.HasAuthority>()
    11.         .ForEach((in Translation playerPos) =>
    12.         {
    13.             var camera = Camera.main;
    14.             if (camera == null) return;
    15.  
    16.             Vector3 targetPos = (Vector3) playerPos.Value + posOffset;
    17.             if (needInit)
    18.             {
    19.                 camera.transform.position = targetPos;
    20.                 camera.transform.rotation = angleOffset;
    21.                 needInit = false;
    22.             }
    23.             else
    24.             {
    25.  
    26.                 camera.transform.position = targetPos;
    27.             }
    28.  
    29.         }).Run();
    30.     }
    31.  
    Anyone knows why? The only possibility comes to my head is that Entities.Foreach take the Translation component data at some point in player loop that I didn't aware of. Thanks!
     
  2. No3371

    No3371

    Joined:
    Dec 1, 2016
    Posts:
    42
    I figured it's one of the system from my colleague updating at wrong order, though this can not explain the funky random behaviour that sometimes it worked fine, I'll take it resolved.