- Home /
Why won't my character mesh move?
Hi, I'm pretty new to Unity and I have a question about an enemy NPC that I made, that's supposed to chase the player when they get within a certain range. The NPC has a spawning animation, that plays correctly, and then an idle and a run animation. I've been trying to get it to stay in the idle animation when not chasing the player and go into its run cycle when it is chasing the player. But right now it just spawns, goes into it's idle animation for a second and then straight into its run cycle, all while staying in place, even with my Follow_player script attached to it. I've attached this script to other objects that don't have multiple clips to their animations and it seems to work the way I want it, but for some reason it's not working here. This is my first time using the Animator thing and I've set it up to use transitions between each clip and everything, but I'll admit I don't know for certain what I'm doing, so I'll include the follow script here as well as a picture of my Animator node setup. Any help or links to tutorials that cover this would be really appreciated, I've been googling it all day and I can't seem to find an answer to this problem specifically.
Here's my follow script:
var target : Transform; //the enemy's target var moveSpeed = 3; //move speed var rotationSpeed = 3; //speed of turning var range : float=10f; var range2 : float=10f; var stop : float=0f; var myTransform : Transform; //current transform data of this enemy function Awake() { myTransform = transform; //cache transform data for easy access/preformance }
function Start() { target = GameObject.FindWithTag("Player").transform; //target the player
}
function Update () { //rotate to look at the player var 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), rotationSpeed*Time.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);
}
}