- Home /
Enemy AI Animation
I stole this AI script from another forum page. Basically the enemy moves towards you when you are within a certain range. I want to know how to use the animator to play a walk animation when the character is moving, and an idle animation when the character is not. What parameters should I set in the animator? I am a beginner when it comes to java script. Thanks!
public var target : Transform;
public var moveSpeed : float = 3.0;
public var attackRange : float = 10.0;
public var stopRange : float = 2.0;
function Start(){
Patrol();
animation.wrapMode = WrapMode.Loop;
animation["idle"].layer = 1;
animation["walk"].layer = 2;
animation.Stop();
}
function Patrol(){
while(true){
if( AttackRangeCheck() ){ // target object is within range
if( !StopRangeCheck() ){
transform.LookAt(target);
transform.Translate(Vector3.forward*Time.deltaTime*moveSpeed);
}
}
yield;
}
}
function AttackRangeCheck() : boolean{
var distanceToTarget = Vector3.Distance(transform.position, target.position);
if(distanceToTarget <= attackRange)
return true;
else
return false;
}
function StopRangeCheck() : boolean{
var distanceToTarget = Vector3.Distance(transform.position, target.position);
if(distanceToTarget <= stopRange)
return true;
else
return false;
}
Answer by HuskyPanda213 · Nov 11, 2013 at 05:06 PM
Use charactercontroller.velocity.magnitude it works well, see if you can use that instead in a if loop.
Your answer
Follow this Question
Related Questions
my AI(soldier) follow me but without animation ? 0 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Adding mecanim animations to an AI follow script 0 Answers
ai navigation help 0 Answers