Search Unity

Mecanim restricting animation problem

Discussion in 'Animation' started by Paparakas, Nov 7, 2013.

  1. Paparakas

    Paparakas

    Joined:
    Feb 28, 2013
    Posts:
    90
    I have a gun model with a shoot, idle and reload animation. I've made it so that when you're reloading, you can't shoot. I want to make it so that if you reload while the shooting animation is ongoing, it'll stop shooting and start reloading. My problem is that it's doing that, but the reload animation doesn't finish. Right now, it starts the reloading animation, the animation plays for about 0.3 seconds, then it goes back to shooting. Here's my state machine: http://i.imgur.com/YCedlvn.png?1?2660 And here is my script:

    Code (csharp):
    1. currentBaseState = anim.GetCurrentAnimatorStateInfo(0);
    2.  
    3.        if(canShoot  Ammunition > 0)
    4.        {
    5.          if(Input.GetButton("Fire1"))
    6.          {
    7.           anim.SetBool("Shot",true);
    8.           Shoot();
    9.          }
    10.        }
    11.        if(currentBaseState.nameHash == shootState)
    12.         {
    13.            anim.SetBool("Shot", false);
    14.            MuzzleFlashRenderer.enabled = false;
    15.           ShootLight.enabled = false;
    16.         }
    17.  
    18.        if(Input.GetButtonDown("Reload")  Clips > 0)
    19.        {
    20.          anim.SetBool("Reload",true);
    21.          Reload();
    22.        }
    23.        if(currentBaseState.nameHash == reloadState)
    24.        {
    25.          anim.SetBool("Reload",false);
    26.          canShoot = false;
    27.        }
    28.        if(currentBaseState.nameHash == idleState)
    29.        {
    30.          canShoot = true;
    31.        }
    I've had some luck by putting a canShoot = false; statement like so:

    Code (csharp):
    1. if(Input.GetButtonDown("Reload")  Clips > 0)
    2.        {
    3.          canShoot = false;
    4.          anim.SetBool("Reload",true);
    5.          Reload();
    6.        }
    But it only works 1/3th of the time.
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,429
    Do you have any transitions that are flagged as "atomic"? The "atomic" flag means "cannot be interrupted" so the animation plays all the way through. This also affects detecting and processing any variables that control state changes.