Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Enemy jitters when camera follows the player

Discussion in 'Scripting' started by unity_8I_suMy7c9iU0g, Dec 12, 2022.

  1. unity_8I_suMy7c9iU0g

    unity_8I_suMy7c9iU0g

    Joined:
    May 29, 2020
    Posts:
    22
    I have an enemy that follows or retreats from player based on the distance.

    It all works fine until i make camera follow the player. When it does, enemy starts jittering while moving.

    Here's the scripts

    Player Movement:

    Code (CSharp):
    1. public class Movement : MonoBehaviour
    2. {
    3.     public float moveSpeed = 5;
    4.  
    5.     private Rigidbody2D rb;
    6.     [SerializeField] private Camera cam;
    7.  
    8.     private Vector2 movement;
    9.     private Vector2 mousePos;
    10.  
    11.     private void Start()
    12.     {
    13.         rb = GetComponent<Rigidbody2D>();
    14.         cam = FindObjectOfType<Camera>();
    15.     }
    16.  
    17.     private void Update()
    18.     {
    19.         movement.x = Input.GetAxisRaw("Horizontal");
    20.         movement.y = Input.GetAxisRaw("Vertical");
    21.  
    22.         mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
    23.     }
    24.  
    25.     private void FixedUpdate()
    26.     {
    27.         rb.AddForce(movement * moveSpeed, ForceMode2D.Impulse);
    28.  
    29.         Vector2 lookDir = mousePos - rb.position;
    30.         float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90;
    31.         rb.rotation = angle;
    32.     }
    33. }
    34.  
    Camera follow:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CamScript : MonoBehaviour
    6. {
    7.     private Camera cam;
    8.     private Transform playerPos;
    9.  
    10.     private void Start()
    11.     {
    12.         cam = GetComponent<Camera>();
    13.         playerPos = GameObject.FindGameObjectWithTag("Player").transform;
    14.     }
    15.  
    16.     private void Update()
    17.     {
    18.         transform.position = new Vector3(playerPos.position.x, playerPos.position.y, -10);
    19.     }
    20. }
    21.  
    Enemy movement:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Enemy : MonoBehaviour
    6. {
    7.     [SerializeField] float speed = 3;
    8.     public bool isAggro = false;
    9.  
    10.     private Rigidbody2D rb;
    11.     public Transform player;
    12.  
    13.     [Header("Distances")]
    14.     [SerializeField] float followDistance;
    15.     [SerializeField] float retreatDistance;
    16.     private float distance;
    17.  
    18.     private void OnEnable()
    19.     {
    20.         rb = GetComponent<Rigidbody2D>();
    21.     }
    22.  
    23.     private void FixedUpdate()
    24.     {
    25.         if (!isAggro) return;
    26.  
    27.         distance = Vector2.Distance(transform.position, player.position);
    28.  
    29.         if (distance > followDistance)
    30.         {
    31.             rb.MovePosition(Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime));
    32.         }
    33.         else if (distance < retreatDistance)
    34.         {
    35.             rb.MovePosition(Vector2.MoveTowards(transform.position, player.position, -speed * Time.deltaTime));
    36.         }
    37.     }
    38. }
    39.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Camera stuff is pretty tricky... you may wish to consider using Cinemachine from the Unity Package Manager.

    There's even a dedicated forum: https://forum.unity.com/forums/cinemachine.136/

    If you insist on doing it yourself, be sure you follow physics objects from FixedUpdate() or LateUpdate().

    Here's why:

    Here is some timing diagram help:

    https://docs.unity3d.com/Manual/ExecutionOrder.html

    Two good discussions on Update() vs FixedUpdate() timing:

    https://jacksondunstan.com/articles/4824

    https://johnaustin.io/articles/2019/fix-your-unity-timestep
     
    unity_8I_suMy7c9iU0g likes this.
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,917
    Have you turned on Interpolation in the rigidbody settings for your enemy?
     
  4. unity_8I_suMy7c9iU0g

    unity_8I_suMy7c9iU0g

    Joined:
    May 29, 2020
    Posts:
    22
    I installed cinamachine and set it to follow the player from inspector, but the enemy still jitters, does that mean the problem is in the enemy movement?
     
  5. unity_8I_suMy7c9iU0g

    unity_8I_suMy7c9iU0g

    Joined:
    May 29, 2020
    Posts:
    22
    Yes, i tried also switching it to extrapolate but it only helped a little
     
  6. unity_8I_suMy7c9iU0g

    unity_8I_suMy7c9iU0g

    Joined:
    May 29, 2020
    Posts:
    22
    Ok so i spent 3 hours trying everything and of course the solution was super simple.

    Turns out the player himself didn't have any interpolation, set it to Interpolate and everything is fixed!
     
    spiney199 and Kurt-Dekker like this.