Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Play entire jump animation before returning to idle

Discussion in 'Scripting' started by MarkVatani, Mar 6, 2015.

  1. MarkVatani

    MarkVatani

    Joined:
    Sep 4, 2012
    Posts:
    39
    How can I get my simple animaton code to play the entire jump animation before returning to run or idle?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SimpleAnimator : MonoBehaviour {
    5.    
    6.     public Animation animate;
    7.  
    8.     void Start()
    9.     {
    10.         animate = GetComponent<Animation>();
    11.     }
    12.     void Update()
    13.     {
    14.         if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1F)
    15.             animate.CrossFade("run");
    16.         else
    17.             animate.CrossFade("stand");
    18.  
    19.         if (Input.GetKeyDown(KeyCode.Space))
    20.             animate.CrossFade("jump");
    21.     }
    22. }
     
  2. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Is there a reason you don't use an animator and animator controller for animations? I would guess that makes handling all kinds of animations a lot easier and more straightforward.

    I have attached a super small sample project I made for someone else using the default 2d platformer hero from the sample project from Unity. Check out the code and how they have set up the animation, I think it can help you a lot. The basics are the same for a 3D project, in case yours is 3D.

    Edit: As far as the current code goes. Try to add in a Boolean and a check to see if you are jumping or not, and use that too decide whether or not to play the animation. Input.GetKeyDown only returns true for one frame after being pressed, Input.GetKey will show the animation as long as you keep the button pressed.
     

    Attached Files:

    Last edited: Mar 6, 2015
  3. MarkVatani

    MarkVatani

    Joined:
    Sep 4, 2012
    Posts:
    39
    Thank you. I'll check out the sample project. I guess for now, I'm just popping basic animations into my project as place holders while I work on other systems. Eventually, I'll be using an animator, I'm just not yet familiar with them, yet.