Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Play complete Legacy Animation with GetKeyDown

Discussion in 'Animation' started by Disolution, May 14, 2014.

  1. Disolution

    Disolution

    Joined:
    Feb 24, 2014
    Posts:
    71
    Pretty much what the title says, i've already went around unityAnswers trying all different solutions that i was able to find but without avail, can't manage to get it working even with the WRAP.Once and even tried with ClampForever even with StartCoroutine...

    The attack and jump animations never finish...is this a bug within unity? (got the latest free one)

    Code (csharp):
    1.     void Update(){
    2.  
    3.         float move = Input.GetAxis("Horizontal");
    4.         if (move > 0  !facingRight)
    5.             Flip ();
    6.         else if (move < 0  facingRight) {
    7.             move=-move;
    8.             Flip ();
    9.         }
    10.  
    11.         if (Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.D)) {
    12.             anim.CrossFade ("BG_Run01");
    13.             isRunning=true;
    14.         } else {
    15.             anim.CrossFade ("BG_AttackStandy");
    16.             isRunning=false;
    17.             isIdle=true;
    18.         }
    19.  
    20.         if (Input.GetKeyDown (KeyCode.S)) {
    21. //          if(isRunning){
    22. //              anim["BG_RunAttack"].wrapMode = WrapMode.Once;
    23. //              anim.Play("BG_RunAttack",PlayMode.StopAll);
    24. //          }else if(isIdle){
    25.             StartCoroutine(COtaunt());
    26. //          }      
    27.         }
    28.  
    29.  
    30.         if (Input.GetKeyDown (KeyCode.Space)) {
    31.             anim.Play("BG_Jump");      
    32.         }
    33.     }
    34.    
    35.  
    36.     private IEnumerator COtaunt () {
    37.         AnimationState state = anim["BG_Attack00"];
    38.         anim.Play("BG_Attack00");
    39.         yield return new WaitForSeconds (state.length);
    40.         animation.CrossFade("BG_AttackStandy");
    41.     }
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    There's a logic issue in your Update() method. When KeyCode.Space is pressed down, it plays BG_Jump.

    The next frame, Update() immediately runs either CrossFade("BG_Run01") or CrossFade("BG_AttackStandy"). This means only 0.3s of BG_Jump will play before it's cross-faded out. Don't cross-fade if the jump animation is playing.
     
  3. Disolution

    Disolution

    Joined:
    Feb 24, 2014
    Posts:
    71
    was able to fix the problem but now i need stop the jump from spamming incorrenctly being that if a player spams the jump it will look right at first but then it starts doing the animation desyncronized with the jumping (starts jump animation from start already on mid air)
    how could i actually force the the animation to play from teh moment that the character finishes the space jump and starts another right away? i tried with ground variables but sometimes it doesnt even turn the boolean ground true beetween the jump spam
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    You can record the length of the jump clip (AnimationState.length) and not allow any other animations to play (at least on that layer) until the clip is done.