Home > Back-end >  Jump Animation Not Displaying in Unity 2D
Jump Animation Not Displaying in Unity 2D

Time:01-19

I'm trying to "animate" (yes, I'm using the animate capabilities of Unity but I'm only changing the sprite from one still frame to another still frame) my character jumping in Unity. According to my code, everything should be running properly but for some reason, nothing happens in-game when I hit the Jump button (player is jumping but the sprite isn't changing). I'm following along with this tutorial and I've tried several others explaining how to use Unity's Animator and Animation windows but nothing seems to be working to change the sprite.

My code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{

    [SerializeField]
    private float moveForce = 10f;

    [SerializeField]
    private float jumpForce = 11f;

    private float movementX;

    private float movementY;

    private Rigidbody2D myBody;

    private SpriteRenderer sr;

    private Animator anim;

    private string WALK_ANIMATION = "Walk";

    private bool isGrounded = true;

    private string GROUND_TAG = "Ground";

    private string JUMP_ANIMATION = "Jump";

    private void Awake()
    {

        myBody = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();

        sr = GetComponent<SpriteRenderer>();

    }
    

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        PlayerMoveKeyboard();
        AnimatePlayer();
        PlayerJump();
        
    }

     void FixedUpdate()
    {
        
    }

    void PlayerMoveKeyboard()
    {
        movementX = Input.GetAxisRaw("Horizontal");

        movementY = Input.GetAxisRaw("Vertical");

        transform.position  = new Vector3(movementX, 0f, 0f) * Time.deltaTime * moveForce;
    }

   

    void AnimatePlayer()
    {
        if (movementX > 0)
        {
            anim.SetBool(WALK_ANIMATION, true);
            sr.flipX = false;
        }
        else if (movementX < 0)
        {
            anim.SetBool(WALK_ANIMATION, true);
            sr.flipX = true;
        }
        else
        {
            anim.SetBool(WALK_ANIMATION, false);
        }

    }
    void PlayerJump()
        {

            if (Input.GetButtonDown("Jump") && isGrounded)
            {
                isGrounded = false;
                myBody.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
                anim.SetBool(JUMP_ANIMATION, true);
                Debug.Log("You should be jumping.");
            } 

        }

    private void OnCollisionEnter2D(Collision2D collision)
    {

        if(collision.gameObject.CompareTag(GROUND_TAG))
        {
            isGrounded = true;
            anim.SetBool(JUMP_ANIMATION, false);
        }

    }
} 

My animator states currently:

I'm not sure if this is enough information or if I've included screenshots of everything so please feel free to ask for more input.

I have tried:

  • Changing the Transitions from (currently) Walking to Jumping to (previously) Idle to Jumping.

  • Removing the Walking animation entirely from the code

  • Removing the Transitions from Walking to Idle so that there were only transitions from Walking to Jumping

  • Checking "Has Exit Time" and unchecking "Has Exit Time"

  • Extending the length of the Jumping animation

  • Changing the speed of the jump from 1 to .5 and from 1 to 2

  • Using an if/else statement in the Animate Player function where the bool of JUMPING_ANIMATION only becomes true if the Y value of the player is higher than the base position when they're standing on the ground

  • (Trying to; not sure if I coded this correctly because I'm shaky on using triggers instead of bools) Use a trigger instead of a bool for initializing the jump animation

I'm absolutely positive I've just missed something in the tutorial or something else fairly simple and dumb but I cannot for the life of me figure out what it is I'm missing. I've searched the other jump animation issues here on SO, too, and none of them seem to be quite what I'm missing. As it stands, my code is telling me that the Jump parameter is becoming true properly based on the Console Log but nothing about the visual sprite changes for the character.

CodePudding user response:

As I can see in your Animator, you need to set your "StateMachine Default State" to one of your used states (I suppose for your case, the Idle State). You can do that with a right click on the Entry State.

  • Related