Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Help Wanted Enemy is jittering when camera is moving

Discussion in 'Navigation' started by Ketaka, Jul 16, 2020.

  1. Ketaka

    Ketaka

    Joined:
    Apr 6, 2020
    Posts:
    2
    Hello,

    I started a test project and failed to properly implement a following enemy towards my character.

    When I move my character my camera follows and the enemy is jittering when moving towards my character.
    The Enemy's movement is smooth when the camera is static.

    Do you have any idea what's causing this issue ?

    Here's the camera's script
    Code (CSharp):
    1. public class CameraController : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     private Transform target;
    5.  
    6.     [SerializeField]
    7.     private Vector3 offsetPosition;
    8.  
    9.     [SerializeField]
    10.     private Space offsetPositionSpace = Space.Self;
    11.  
    12.     [SerializeField]
    13.     private bool lookAt = true;
    14.  
    15.     private void LateUpdate()
    16.     {
    17.         Refresh();
    18.     }
    19.  
    20.     public void Refresh()
    21.     {
    22.         if (target == null)
    23.         {
    24.             Debug.LogWarning("Missing target ref !", this);
    25.  
    26.             return;
    27.         }
    28.  
    29.         // compute position
    30.         if (offsetPositionSpace == Space.Self)
    31.         {
    32.             transform.position = target.TransformPoint(offsetPosition);
    33.         }
    34.         else
    35.         {
    36.             transform.position = target.position + offsetPosition;
    37.         }
    38.  
    39.         // compute rotation
    40.         if (lookAt)
    41.         {
    42.             transform.LookAt(target);
    43.         }
    44.         else
    45.         {
    46.             transform.rotation = target.rotation;
    47.         }
    48.     }
    49. }
    And the Enemy's script :
    Code (CSharp):
    1. public class enemyAi : MonoBehaviour
    2. {
    3.  
    4.     public Transform Target;
    5.     public UnityEngine.AI.NavMeshAgent agent;
    6.  
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         agent = gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>();
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void FixedUpdate()
    15.     {
    16.         agent.destination = Target.position;
    17.     }
    18. }
    19.  
     
  2. Ketaka

    Ketaka

    Joined:
    Apr 6, 2020
    Posts:
    2
    Fixed it by lowering my Fixed Timestep from 0.02 to 0.0075
    But I feel i'm missing something and it's just a hot fix. I'll see later I guess.
     
unityunity