Search Unity

GetKeyDown input only plays first frame of animation

Discussion in 'Scripting' started by kenmarold, Dec 31, 2015.

  1. kenmarold

    kenmarold

    Joined:
    Jun 11, 2015
    Posts:
    27
    I'm trying to control a set of character animations but I'm having trouble with the jump animations. My horizontal walk animations are working fine but when I hit the jump button I want one of two jump animations to play depending on whether the player is in an idle or walking state. However when I press the jump button currently it only plays the first frame of the respective jump animation and then goes right back to either idle or walk. How do I get the entire jump animation to play when the jump button is pressed?

    using UnityEngine;
    using System.Collections;

    public class AsherBlackMover : MonoBehaviour {

    public float moveSpeed = 3;
    public float rotateSpeed = 10;
    public Transform graphics;
    public SkeletonAnimation skeletonAnimation;
    public Vector2 jumpVector;
    public bool isGrounded;

    // isGrounded Variables
    public Transform grounderPosition;
    public float grounderRadius;
    public LayerMask grounderLayerMask;

    private Rigidbody2D asherBlackRB;
    private Animator asherBlackAnim;

    Quaternion goalRotation = Quaternion.identity;
    float xDir;
    float yDir;
    string currentAnimation = "";

    void Start ()
    {
    // Create a reference to Asher Black Rigidbody2D
    asherBlackRB = GetComponent<Rigidbody2D>();
    }

    void Update ()
    {
    xDir = Input.GetAxis ("Horizontal") * moveSpeed;

    if (xDir > 0) // ------ Walk Right
    {
    goalRotation = Quaternion.Euler (0, 0, 0);
    SetAnimation ("Walk", true);
    }
    else if (xDir < 0) // ------ Walk Left
    {
    goalRotation = Quaternion.Euler (0, 180, 0);
    SetAnimation ("Walk", true);
    }
    else if (isGrounded == true) // ------ Idle
    {
    SetAnimation ("Idle", true);
    }

    // Jump Button
    if (Input.GetKeyDown("space") && isGrounded == true)
    {
    isGrounded = false;

    if (xDir > 0)
    {
    Jump ("Walk-Jump");
    }
    else if (xDir < 0)
    {
    Jump ("Walk-Jump");
    }
    else
    {
    Jump ("Idle-Jump");
    }
    }

    // Circle on character that determines when grounded or not
    isGrounded = Physics2D.OverlapCircle (grounderPosition.transform.position, grounderRadius, grounderLayerMask);

    // Flip character smothly to emulate paper mario effect
    graphics.localRotation = Quaternion.Lerp (graphics.localRotation, goalRotation, rotateSpeed * Time.deltaTime);
    }

    void OnDrawGizmos ()
    {
    Gizmos.color = Color.yellow;
    Gizmos.DrawWireSphere (grounderPosition.transform.position, grounderRadius);
    }

    void SetAnimation (string name, bool loop)
    {
    if (name == currentAnimation)
    {
    return;
    }
    skeletonAnimation.state.SetAnimation (0, name, loop);
    currentAnimation = name;
    }

    void Jump (string animName)
    {
    asherBlackRB.AddForce (jumpVector, ForceMode2D.Force);
    SetAnimation (animName, true);
    }

    // Physics Updates
    void FixedUpdate ()
    {
    asherBlackRB.velocity = new Vector2 (xDir, asherBlackRB.velocity.y);
    }
    }
     
  2. Locky

    Locky

    Joined:
    May 14, 2015
    Posts:
    86
    I am no expert in C#; pretty new to it actually. But I think it has to do with your setting isGrounded to false.

    The animation starts to play because isGrounded is true. Then it gets set to false immediately before it has a chance to get to the next frame.

    The animation needs to be allowed play to the end before you set isGrounded back to true.