Hi,
I made a little AI/animation script (loosely based on the one from the fps tutorial)
When the ennemy is attacking, if the player move away, the ennemy follow him, but with the attack animation still runing, then when this animation is finished and the player is still away, he switch to the run animation.
Here is my code :
(for the sake of clarity I only put the two functions where the problem is )
function Update()
{
if (CanSeeTarget ())
{
searchTimeout=5.0;
attacking = true;
lastVisiblePlayerPosition = target.position;
var distance = Vector3.Distance(transform.position, target.position);
if (distance > dontComeCloserRange)
{
MoveTowards (lastVisiblePlayerPosition);
childModel.animation.CrossFade("run1");
}
else
{
RotateTowards(lastVisiblePlayerPosition);
if(lastShot<=0)
{
lastShot=childModel.animation["attack"+GetComponent("AIAnimationZombie").GetAttackAnim()].length;
Shoot();
}
lastShot-=Time.deltaTime;
}
}
else
{
if(attacking)
{
if(searchTimeout<=0)
{
attacking=false;
childModel.animation.CrossFade("idle1");
}
else
{
//Debug.Log("searchTimeout : "+searchTimeout);
SearchPlayer (lastVisiblePlayerPosition);
childModel.animation.CrossFade("run1");
}
searchTimeout-=Time.deltaTime;
}
}
}
function Shoot () {
childModel.animation.Stop();
childModel.animation.CrossFade("attack"+GetComponent("AIAnimationZombie").GetAttackAnim(),0.01);
yield WaitForSeconds(childModel.animation["attack"+GetComponent("AIAnimationZombie").GetAttackAnim()].length/2);
//play sound
if(attackSound)
AudioSource.PlayClipAtPoint(attackSound, transform.position);
// Fire gun
BroadcastMessage("Fire", target);
}
What I want to achieve in this case is to stop directly the attack animation and switch to the run animation OR that the ennemy don’t move until the attack animation is finished.
I don’t know if it’s understable, but I don’t know how to explain it another way
Thanks for your help