Search Unity

Playing the jump animation twice, why?

Discussion in 'Scripting' started by ma.parizeau, Dec 6, 2010.

  1. ma.parizeau

    ma.parizeau

    Joined:
    Mar 21, 2010
    Posts:
    116
    Hi everyone.

    In my script, I only have one place that mentions the jump animation.

    Code (csharp):
    1.  
    2.         if(Input.GetButtonDown("Jump"))
    3.         {  
    4.             animation.CrossFade("jump");
    5.             moveDirection.y = jumpSpeed;
    6.         }
    7.  
    My problem is that when I press the space bar, which have the default value on the input button, it plays the animation twice.

    why would that be?
     
  2. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Do you have the script attached twice?
     
  3. ma.parizeau

    ma.parizeau

    Joined:
    Mar 21, 2010
    Posts:
    116
    It's only on once. All of the other animation are playing correctly. Also i,ve looked in 3DS MAX and the animation only plays once with the frames that I've marked down.
     
  4. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Interesting. I don't really have any ideas then, unfortunately.
     
  5. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    I believe that you may be calling the jump animation because you are holding down the space bar, and your not telling the game that you have jumped.

    Observe the Pseudo code:
    Code (csharp):
    1.  
    2. var isGrounded=false;
    3. function Update(){
    4. // dont let us jump unless we are on the ground
    5. if(Input.GetButtonDown("Jump")  isGrounded)
    6. {  
    7. animation.CrossFade("jump");
    8. moveDirection.y = jumpSpeed;
    9. // if we jumped, then we are no longer grounded
    10. isGrounded=false;
    11. }
    12.  
    13. // check to see if we are on the ground
    14. if(checkToSeeIfWeAreGrounded()){
    15. isGrounded=true;
    16. }
    17. }
    18.  
    Check over the 3rd person controller in the standard assets.
     
  6. ma.parizeau

    ma.parizeau

    Joined:
    Mar 21, 2010
    Posts:
    116
    Thanks, bigMisterB, I will try it out.
     
  7. shabazzster

    shabazzster

    Joined:
    Mar 17, 2010
    Posts:
    104