Stop a function?

Ok, so I have this little problem which I think can be solved by stopping a function while it’s still active.

I have an animation (shoot1) which aims forward and it takes 0.333 to get to the last animation frame. That’s when I want it to shoot. I tried many different ways to make it shoot after getting to the last frame and this is the one I’m trying now.

var shooting : boolean = false;


function Update () {

if (Input.GetButtonDown ("Fire1")) {
animation.Play("shoot1");

}

if (Input.GetButton("Fire1"))
{
Shooting1();
}

if (Input.GetButtonUp ("Fire1")) {
animation.Play("idle");
shooting = false;
}

function Shooting1(){
yield WaitForSeconds(0.333);
shooting = true;
}

Now the problem is that even after I release the fire button, the “shooting” boolean remains true. I’m guessing it actually turns false but then 0.333 seconds later it turns true again (because the Shooting1 function hadnt finished it’s job).
Does anybody know how to fix this?
And in case this is a dead end, what kind of approach should I take to shoot during the last animation frame?

Why not just poll the animation to see if it is playing? If you want it after the last frame try this:

var playingShootAnim : boolean = false;
 
 
function Update () {

if(playingShootAnim && !animation.IsPlaying("shoot1"))
{
    playingShootAnim = false;
    //shoot
    //back to idle
}

if (Input.GetButtonDown ("Fire1")) {
    animation.Play("shoot1");
    playingShootAnim = true;     
}

This is a quick simple fix, but there are much more elegant solutions :slight_smile:

EDIT:

Just such a better solution - no bools, just fire when done:

EDIT EDIT:
put another condition so you won’t be overriding you animation, causing a stutter. If you do want to override the animation, just check for button down only.

function Update () {

    if (Input.GetButtonDown ("Fire1")  && !animation.IsPlaying("shoot1"))
    {
        animation.Play("shoot1");
        Shoot1();
    }        

}

function Shooting1(){
{
    while (animation.IsPlaying("shoot1"))
    {
        yield;
    }
    // shoot
    yield break;
}

So essentially the same as the previous code, just with out causing it to ALWAYS be in update.

I would use Invoke which calls the specified method in N seconds.

You could use it like:

var shooting : boolean = false;
 
 
function Update () {
 
    if (Input.GetButtonDown ("Fire1")) {
      animation.Play("shoot1");
 
    }
 
    if (Input.GetButton("Fire1"))
    {
        Invoke("Shoot", 0.333f);
    }
 
    if (Input.GetButtonUp ("Fire1")) {
       animation.Play("idle");
       shooting = false;
    }
 }
    function Shoot(){
        shooting = true;
    }
}

I would change the hard-coded 0.333f though and put it equal to your animation length so that if you change the animation, it still works as intended.

Quick and dirty solution, but perhaps have the update method have something like:

if(!Input.GetButton("Fire1")){
   shooting = false;
   }

so that it keeps resetting it to false.