Get the distance that an AI is away from a Player?

I have a script where when I get within 75f of a mummy AI, the mummy turns and looks at me. When I get within 25f, the mummy starts to chase me. When I get to 3f, the mummy stops.

I want to set up animations for this mummy using an Animator, so when he’s 75 away, he draws his weapon, when he’s 25 away, he starts walking, and when he’s 3 away he starts to attack.

I have the animations set up in the animator, all I need is sufficient parameters that allow the AI to switch animations based on that.
Here’s my script so far;

using UnityEngine;
using System.Collections;
public class AIFollowScript : MonoBehaviour {
Animator EnemyAnimationTest;
public bool Walking = false;
public bool Standing = true;
public bool Attacking = false;
Transform target;
public float moveSpeed = 7f;
public float rotationSpeed = 7f;
public float Target;
public float Thedistance;
public float range = 25f;
public float range2 = 75f;
public float stop = 3f;
Transform myTransform;
void Awake (){
myTransform = transform;
}

void Start (){
target = GameObject.FindWithTag(“Player”).transform;
EnemyAnimationTest = GetComponent();
}

void Update (){
var Walk = range;
var Idle = range2;
var Attack = stop;
//Rotate towards the player
float distance= Vector3.Distance(myTransform.position, target.position);
if (distance<=range2 && distance>=range){
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
}

else if(distance<=range && distance>stop){

//move towards the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeedTime.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
else if (distance<=stop) {
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed
Time.deltaTime);
}
}
void FixedUpdate (){
EnemyAnimationTest.SetFloat (“Walk”, range);
EnemyAnimationTest.SetFloat (“Run”, range2);
EnemyAnimationTest.SetFloat (“Attack”, stop);
}
void Walk (){
if (Thedistance = range)
Walking = true;
Standing = false;
Attacking = false;
}
void Idle (){
if (Thedistance = range2)
Walking = false;
Standing = true;
Attacking = false;
}
void Attack (){
if (Thedistance = stop)
Walking = false;
Standing = false;
Attacking = true;
}
}

Any help would be greatly appreciated, thanks.

Please use code tags.

Your AI is essentially a finite state machine (FSM). It’s in one state at a time: Draw, Walk, or Attack. It transitions to different states based on distance from the player.

Animator controllers are FSMs, so you can build the logic into your animator controller.

Create three animator states: Draw, Walk, and Attack.

Transition from Draw to Walk when distance is less than 25.

Transition from Walk to Attack when distance is less than 3.

Note that this is all done visually, in the Animator view.

Your script in this case will be very simple. Every Update(), simply set the animator’s distance parameter:

void Update() {
    animator.SetFloat("distance", Vector3.Distance(transform.position, target.position));
}

If you want to attach more AI behavior to each state, add a StateMachineBehaviour.