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

Bug Enemy AI not working properly, constantly moving toward player

Discussion in 'Scripting' started by corruptRadiance, Sep 30, 2021.

  1. corruptRadiance

    corruptRadiance

    Joined:
    Sep 17, 2021
    Posts:
    6
    (This is my first forum post so forgive me for any mistakes)

    Hi, I'm trying to write an enemy AI script that leaves the enemy stationary until the player enters their line of sight, then draws a raycast from an empty "LookAtPlayer" object at the feet of the enemy towards the player, to make sure the player is actually within line of sight (AKA not obstructed by any surfaces or out of range).

    In my head this code works perfectly, but in practice it causes a bug where the enemies slowly move towards the player until they enter line of sight, THEN they move normally. Any help on how I could squash this bug would be highly appreciated. I'll provide a video displaying the bug in action.



    Code (CSharp):
    1. void Update()
    2.     {
    3.         // Calculate angle of Enemy transform forward to Player transform forward
    4.         // If angle < AllowedAngle, Raycast forward while looking at player
    5.         float angle = Vector3.Angle(transform.forward, Player.transform.forward);
    6.         if (angle < AllowedAngle)
    7.         {
    8.             // If raycast hits player, enter pursuit mode
    9.             LookAtPlayer.transform.LookAt(Player.transform.position);
    10.             if (Physics.Raycast(LookAtPlayer.transform.position, LookAtPlayer.transform.forward, AllowedRange, LayerMask) && !attackTrigger)
    11.             {
    12.                 transform.position = Vector3.MoveTowards(transform.position, Player.transform.position, Speed * Time.deltaTime);
    13.             }
    14.         }
    15.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Are there Rigidbodies on the enemies? When you use
    transform.LookAt()
    you are bypassing the physics system and in the reconciliation step the physics will nudge and noodle around a bit.

    If that's what you're doing, then to fix it, use .MoveRotation() and .MovePosition() on the Rigidbody.

    If that's not it, then you still must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  3. corruptRadiance

    corruptRadiance

    Joined:
    Sep 17, 2021
    Posts:
    6
    Thanks for the reply! I'm trying to give .MoveRotation() a go, but Quaternions seem a little beyond me. I'm not sure how to make the LookAtPlayer object move relative to the player position. Any tips on that would be appreciated, but all the advice you gave is already more than enough.
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    What you need to do is subtract the two positions to get a vector going from one to the other, then use this:

    https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html

    That gives you a Quat back which you can give to MoveRotation().

    I believe it is effectively the same rotation you get by calling .LookAt() on the Transform itself.
     
    Last edited: Sep 30, 2021
  5. corruptRadiance

    corruptRadiance

    Joined:
    Sep 17, 2021
    Posts:
    6
    Awesome! I'll try implementing that in the morning.

    Thanks so much for helping!