Help Checking Enemy distance from player to make idle work

So my enemy stays in running animation with this script here that I picked up off a dev tutorial so how can I get the enemy to go back to Idle and stop moving after player after visDist is greater for the two objects, like can I add check to see if visDist is greater or what?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AIController : MonoBehaviour
{
    Animator anim;
    public Transform player;
    public float rotationSpeed = 2.0f;
    public float speed = 2.0f;
    public float visDist = 20.0f;
    public float visAngle = 30.0f;
    public float shootDist = 5.0f;
  

    string state = "IDLE";

    void Start()
    {
        anim = this.GetComponent<Animator>();
    }

    void Update()
    {
        Vector3 direction = player.position - this.transform.position;
        float angle = Vector3.Angle(direction, this.transform.forward);

        if(direction.magnitude < visDist && angle < visAngle)
        {
            direction.y = 0;

            this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), Time.deltaTime * rotationSpeed);

            if(direction.magnitude > shootDist)
            {
                if(state != "RUNNING")
                {
                    state = "RUNNING";
                    anim.SetTrigger("isRunning");
                }
              
              
            }
            else
            {
                if(state != "SHOOTING" && direction.magnitude < shootDist)
                {
                state = "SHOOTING";
                anim.SetTrigger("isShooting");
                }
            }
        }
        else
        {
            if(state != "IDLE")
            {
                state = "IDLE";
                anim.SetTrigger("isIdle");

            }
          

        }
        if(state == "RUNNING")
            this.transform.Translate(0,0, Time.deltaTime * speed);
    
    }
}

That seems reasonable but there are a lot of if/else branches above for all the states the thing can be in. If you are unsure how they interact, you’re going to have to figure that out first or else just checking distance isn’t going to be trivial to add.

You 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://discussions.unity.com/t/839300/3

You know I’ve been thinking about debug earlier and that’s good point. Like dealing with computers in general is just problem solving so if I can capitalize on better trouble shooting, then I can save time in the long run. Good idea.