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), rotationSpeedTime.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.