Jumping Animation

I created a jumping animation Rick did not. now I am trying to make my character to only do the animation once while jumping. I read the other jumping animation question on Nov. 21 2021 and I have a similar issue. My character does not stop the animation when he hits the ground. Any ideas?

Hi,

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Did you define an exit condition for the jumping state in the animator?

Hope this helps :slight_smile:


See also;

1 Like

β€˜β€™β€™
using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.InputSystem;

public class PlayerMovement : MonoBehaviour

{

[SerializeField] float runSpeed = 10f;

[SerializeField] float jumpSpeed = 20f;

CapsuleCollider2D playerCapsuleCollider;

 

Vector2 moveInput;

Rigidbody2D playerRigidbody;

Animator playerAnimator;

void Start()

{

    playerRigidbody = GetComponent<Rigidbody2D>();

    playerAnimator = GetComponent<Animator>();

    playerCapsuleCollider = GetComponent<CapsuleCollider2D>();

}



void Update()

{

    if (!playerCapsuleCollider.IsTouchingLayers(LayerMask.GetMask("Ground")))

   {

       playerAnimator.SetTrigger("isJumping");

   }

    Run();

    FlipSprite();

}

void OnMove(InputValue value)

{

    moveInput = value.Get<Vector2>();

    //playerAnimator.SetBool("isJumping", false);

}

    void OnJump(InputValue value)

{

   if (!playerCapsuleCollider.IsTouchingLayers(LayerMask.GetMask("Ground")))

   {

       //playerAnimator.SetTrigger("isJumping");

       return;

   }

    if (value.isPressed)

    {

        playerRigidbody.velocity += new Vector2 (0f, jumpSpeed);

        //playerAnimator.SetTrigger("isJumping");

    }

}

    void Run()

{

    Vector2 playerVelocity = new Vector2 (moveInput.x * runSpeed, playerRigidbody.velocity.y);

    playerRigidbody.velocity = playerVelocity;

    bool playerHasHorizontalSpeed = Mathf.Abs(playerRigidbody.velocity.x) > Mathf.Epsilon; //Find the velocity of your movement

   

    if (playerHasHorizontalSpeed)

    {

        playerAnimator.SetBool("isRunning", true);

    }

    else

    {

        playerAnimator.SetBool("isRunning", false);

    }

}

void FlipSprite()

{

    bool playerHasHorizontalSpeed = Mathf.Abs(playerRigidbody.velocity.x) > Mathf.Epsilon; //Find the velocity of your movement

    if (playerHasHorizontalSpeed)

    {

    transform.localScale = new Vector2 (Mathf.Sign(playerRigidbody.velocity.x),1f);

    }

}

}
β€˜β€™β€™

I figured out the issue and i have a solution.

void IsJumping()

{

    if (!playerCapsuleCollider.IsTouchingLayers(LayerMask.GetMask("Ground")))

   {

       playerAnimator.SetBool("isJumping", true);

   }

   else

   {

       playerAnimator.SetBool("isJumping", false);

   }

}

void Update()

{

    Run();

    FlipSprite();

    IsJumping();

}

Good job! :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms