- Home /
Enemy AI Animation
Basically, I want my AI to play his "run" animation when running towards the player, and the "Idle" animation (or Take 001 in this case) when he isn't. Here's my script, currently, he will run up the player, and stop, but continue his run animation.
public var target : Transform; public var moveSpeed : float = 3.0; public var attackRange : float = 10.0; public var stopRange : float = 2.0; private var isRunning:boolean = true;
function Start(){ Patrol(); animation.wrapMode = WrapMode.Loop; animation["Take 001"].layer = 1; animation["run"].layer = 2; animation.Stop(); }
function Update() { if(isRunning == false){ animation.CrossFade("Take 001"); } if(isRunning == true) { animation.CrossFade("run"); } var distanceToTarget = Vector3.Distance(transform.position, target.position); if(distanceToTarget <= stopRange) { isRunning = false; animation.CrossFade("Take 001"); } }
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 doomprodigy · Feb 23, 2011 at 12:20 AM
Unsure on what the cause is, though make it go
function Update(){
if(isRunning == true) { animation.CrossFade("run"); }
else if(isRunning == false) {
animation.CrossFade("Take 001");
}
var distanceToTarget = Vector3.Distance(transform.position, target.position);
if(distanceToTarget <= stopRange)
{
isRunning = false;
animation.CrossFade("Take 001");
} }
That way it does either run or not.
(Unsure if it fixes your problem).
May I also add that when I script AI functions use a finte state machine. Or just use states/cases inside the same script.
Example:
enum animals { dog, cat, bird }
function Start(){
var animals : animals = animals.dog;
switch(choice) {
case animals.dog:
Debug.Log("dog");
break;
case animals.cat:
Debug.Log("cat");
break;
case animal.bird:
Debug.Log("bird");
break;
default:
Debug.Log("nothing");
break;
}
Peace, Aaron Lewis
The first insight for a "state machine" is that ins$$anonymous$$d of using isIdle, isRunning, isAttacking... you can make one variable AIstate and use 0=idle, 1=running, 2=attacking. ins$$anonymous$$d of setting run=false and attack=true, you can simply change AIstate=2. An enum is a way to "name" 0 is idle, and so on, so you can write AIstate=IDLE
Answer by Owen-Reynolds · Apr 14, 2011 at 04:25 PM
Put run and Take001 on the same layer.
Each layer can play one animation, and higher layers beat lower ones. Since Run is on layer2, starting Take001 on layer 1 ALSO plays it, but the game uses only Run. If you leave them on the same layer, Take001 will automatically cancel (and fade from) Run. You could stop run before starting Take001, but that's looks worse and is more work.
Higher layers are good for a non-looping animations, like shoot. When it stops, the lower level one, which was always secretly playing, "comes back."
You can skip setting layers for Run/Take001, since the starting layer is 0. I also prefer to set Loop individually for each animation clip, in the Inspector for the model. That way you can set shoot or die to non-looping.
See http://unity3d.com/support/documentation/Manual/Animations.html for a better explanation.
Your answer
Follow this Question
Related Questions
Enemy Animation Freezing in first frame 2 Answers
where do i call the Run and Idle animation in this script 2 Answers
play animation when moving,and idle animation when stand still 1 Answer
Idle Animations, Player Walking 0 Answers
Player plays walking animation if I press a and d at the same time while standing still 2 Answers