Search Unity

Animation cancels itself out due to bad code?

Discussion in 'Getting Started' started by MikeTeavee, Jan 16, 2016.

  1. MikeTeavee

    MikeTeavee

    Joined:
    May 22, 2015
    Posts:
    194
    This problem is driving me crazy, perhaps there is a simple solution...

    In my FPS game there is a "Pistol" script that has coroutines for each action, E.G. firing, and reloading. They include the animation, and the animation played in reverse (to return the object to it's original position).

    Each action works fine, but something strange happens with the ReloadGun coroutine... The first animation is skipped, and the only the reverse animation is played. I'm thinking the animation call on line 12 is cancelling out the animation call on line 6 somehow.

    As for the editor&animations themselves, the reload animation is exceptionally longer than the other animations, otherwise I don't see anything out of the ordinary.

    If you need more information, let me know.

    My code for ReloadGun():
    Code (CSharp):
    1. IEnumerator ReloadGun(){
    2.         Debug.Log("Reloading...");
    3.         GunIsFireable = false;
    4.  
    5.         // play animation...
    6.         PistolAnimation.Play("ReloadPistol");
    7.         // ...play animation
    8.  
    9.         // play animation in reverse...
    10.         PistolAnimation["ReloadPistol"].time = PistolAnimation["ReloadPistol"].length;
    11.         PistolAnimation["ReloadPistol"].speed = -1.0f;
    12.         PistolAnimation.Play("ReloadPistol");
    13.         // ...play animation in reverse
    14.  
    15.         PistolAudio[2].Play();
    16.         yield return new WaitForSeconds(2.5f);
    17.         bullets = 10;
    18.         GunIsFireable = true;
    19.     }
    20.  
    21.  


    Also if it helps... code for another coroutine that works fine...


    Code (CSharp):
    1. IEnumerator FireGun(){
    2.         bullets -= 1;
    3.         Debug.Log("Pistol fired!");
    4.         GunIsFireable = false;
    5.  
    6.         // play animation...
    7.         PistolAnimation.Play("Fire");
    8.         // ...play animation
    9.  
    10.         // play animation in reverse...
    11.         PistolAnimation["Fire"].time = PistolAnimation["Fire"].length;
    12.         PistolAnimation["Fire"].speed = -1.0f;
    13.         PistolAnimation.Play("Fire");
    14.         // ...play animation in reverse
    15.  
    16.         PistolAudio[0].Play();
    17.         yield return new WaitForSeconds(0.15f);
    18.         GunIsFireable = true;
    19. }
     
    Last edited: Jan 16, 2016
  2. Manny Calavera

    Manny Calavera

    Joined:
    Oct 19, 2011
    Posts:
    205
    The code you posted is not going to play the animation twice. It will only play the animation once (in reverse). Unless you are missing another 'WaitForSeconds' after the line where you call Play() for the first time.

    But to be honest, you could drop this code entirely and use Mecanim instead. It would be much simpler to achieve what you described above.
     
  3. MikeTeavee

    MikeTeavee

    Joined:
    May 22, 2015
    Posts:
    194
    Thanks for the reply! I guess I needed a 'WaitForSeconds', and apparently needed to set ["ReloadPistol"].speed back to positive 1.0f as well.

    I really wish I knew why the other coroutines work fine the way they are, and why ReloadGun() needed such special attention.:confused:

    I will buckle down and try to improve my ability with Mecanim, it's just daunting because I feel the most comfortable handing things with code.