AI function/coroutine animation issues

Gentlemen.

Currently, I’m writing some comprehensive AI. I’m coding reactions to a large variety of situations, and referring to them within the Update function, but it’s not going so well.

One of the biggest problems I have is with attacking; Despite the fact that the animations have their wrap modes set to Once, they just don’t stop. Even when the function shouldn’t be able to activate because of a boolean check variable. Here’s an example:

This is the bit that checks for certain conditions:

var canAttack = true;

function Update () {
	if(Vector3.Distance(Player.transform.position, transform.position) < playerAttackRad && canAttack)
		Attack1();
}

And this is the Attack function:

function Attack1 () {
	rigidbody.velocity = Vector3.zero;
	yield WaitForSeconds(.5);
	animation.Play("StandAttack1");
	canAttack = false;
	yield WaitForSeconds(.8);
	animation.Stop("StandAttack1");
	dodgeJump();
	yield;
}

As you can see, in order for the function to be triggered, canAttack has to be true, which it starts out as. And then when the function is executed, it’s changed to false, and set back after a certain amount of time.

But despite this, the animation will either not play, play repeatedly, or begin to spaz out after playing correctly the first time. Any wisdom to impart?

EDIT

Here’s the current state of the function, which still isn’t working as it should:

function Attack1 () {
	rigidbody.velocity = Vector3.zero;
	animation.Play("StandAttack1");
	canAttack = false;
	dodgeJump();
}

DOUBLE EDIT

Here’s a video showing off the issue:

As you can see, the animation is playing more than once, which I don’t think it should be able to what with it being set to Once. Also note the spazzing near the end of each loop.

TRIPLE EDIT

Here’s the re-enabling code:

function Update () {
  if(!canAttack)
     AttackEnable();

}

function AttackEnable () {
	yield WaitForSeconds(Random.Range(5,8));
	canAttack = true;

}

You are restarting the coroutine for 0.5 seconds - about 30 times- before you set canAttack to false. That will make for good spazage.

Remove that new Update code and only use (after grokking) this:

function Attack1 ()
{
    rigidbody.velocity = Vector3.zero;
    animation.Play("StandAttack1");
    canAttack = false;
    yield WaitForSeconds(2.0);
    canAttack = true;
}

Notice:

  • set the canAttack guard before yielding
  • let the animation end itself, no need to stop it

I’ve left out dodgeJump(), since I don’t know what it does.