title: How to handle jump animation in Unity?
body: <p>My main issue is how to stop jump animation (after it triggers, it played infinetly).</p>
  <p>This is my code:</p>
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.InputSystem;
  using System;
  
  public class PlayerMovementScript : MonoBehaviour
  {
      [SerializeField] float runSpeed;
      [SerializeField] float jumpSpeed;
  
      Vector2 moveInput;
      Rigidbody2D playerRigidbody;
      Animator playerAnimator;
      CapsuleCollider2D playerCapsuleCollider;
  
      void Start()
      {
          playerRigidbody = GetComponent&lt;Rigidbody2D&gt;();
          playerAnimator = GetComponent&lt;Animator&gt;();
          playerCapsuleCollider = GetComponent&lt;CapsuleCollider2D&gt;();
          
      }
      void Update()
      {
          Run();
          FlipSprite();
      }
  
      void OnMove(InputValue value)
      {
          moveInput = value.Get&lt;Vector2&gt;();
          Debug.Log(moveInput);
      }
  
      void OnJump(InputValue value)
      {
          if(!playerCapsuleCollider.IsTouchingLayers(LayerMask.GetMask(&quot;Ground&quot;))) { return; }
  
          if(value.isPressed)
          {
              playerAnimator.SetBool(&quot;isJumping&quot;, true);
              playerRigidbody.velocity += new Vector2(0f, jumpSpeed);
          }
      }
  
      void Run()
      {
          Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, playerRigidbody.velocity.y);
          playerRigidbody.velocity = playerVelocity;
  
          bool playerHasHorizontalSpeed = Math.Abs(playerRigidbody.velocity.x) &gt; Mathf.Epsilon;
  
          playerAnimator.SetBool(&quot;isRunning&quot;, playerHasHorizontalSpeed);
      }
  
      void FlipSprite()
      {
          bool playerHasHorizontalSpeed = Math.Abs(playerRigidbody.velocity.x) &gt; Mathf.Epsilon;
          if (playerHasHorizontalSpeed) {
              transform.localScale = new Vector2(Mathf.Sign(playerRigidbody.velocity.x), 1f);
          }
      }
  }
  </code></pre>
  <p>Ant this is my current animator:</p>
  <p><a href="https://i.stack.imgur.com/0jZ9O.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0jZ9O.png" alt="enter image description here" /></a></p>
  <p>Bools:
  From Idle to Jump:</p>
  <ul>
  <li>isJumping = true
  From Jump to Idle:</li>
  <li>isJumping = false</li>
  <li>isRunning = false
  From Jump to Run:</li>
  <li>isJumping = false</li>
  <li>isRunning = true
  From Idle to Run:</li>
  <li>isJumping = false</li>
  <li>isRunning = true
  From Run to Idle:</li>
  <li>isRunning = false</li>
  </ul>
  <p>I tried few attempts with isGrounded from this tutorial, but after changes my character can only jump and animation still works uncorrectly:
  <a href="https://www.youtube.com/watch?v=FTxQKHG5WCA" rel="nofollow noreferrer">https://www.youtube.com/watch?v=FTxQKHG5WCA</a></p>
  <p>How to fix that?</p>
  <p>#Update 1:</p>
  <p>I tried this code bellow, but my character only can jump after changes and animation still not working:</p>
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.InputSystem;
  using System;
  
  public class PlayerMovementScript : MonoBehaviour
  {
      [SerializeField] float runSpeed;
      [SerializeField] float jumpSpeed;
  
      [SerializeField] Transform groundCheckCollider;
      [SerializeField] LayerMask groundLayer;
      [SerializeField] bool isGrounded;
      const float groundCheckRadius = 0.2f;
  
      Vector2 moveInput;
      Rigidbody2D playerRigidbody;
      Animator playerAnimator;
      CapsuleCollider2D playerCapsuleCollider;
  
      void Start()
      {
          playerRigidbody = GetComponent&lt;Rigidbody2D&gt;();
          playerAnimator = GetComponent&lt;Animator&gt;();
          playerCapsuleCollider = GetComponent&lt;CapsuleCollider2D&gt;();
          
      }
      void Update()
      {
          Run();
          FlipSprite();
          GroundCheck();
  
          playerAnimator.SetFloat(&quot;yVelocity&quot;, playerRigidbody.velocity.y);
      }
  
      void OnJump(InputValue value)
      {
          if(!playerCapsuleCollider.IsTouchingLayers(LayerMask.GetMask(&quot;Ground&quot;))) { return; }
  
          if(value.isPressed)
          {
              playerAnimator.SetBool(&quot;isJumping&quot;, true);
              playerRigidbody.velocity += new Vector2(0f, jumpSpeed);
          }
      }
  
      void Run()
      {
          Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, playerRigidbody.velocity.y);
          playerRigidbody.velocity = playerVelocity;
  
          bool playerHasHorizontalSpeed = Math.Abs(playerRigidbody.velocity.x) &gt; Mathf.Epsilon;
  
          playerAnimator.SetBool(&quot;isRunning&quot;, playerHasHorizontalSpeed);
      }
  
      void FlipSprite()
      {
          bool playerHasHorizontalSpeed = Math.Abs(playerRigidbody.velocity.x) &gt; Mathf.Epsilon;
          if (playerHasHorizontalSpeed) {
              transform.localScale = new Vector2(Mathf.Sign(playerRigidbody.velocity.x), 1f);
          }
      }
  
      void GroundCheck()
      {
          isGrounded = false;
          Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheckCollider.position, groundCheckRadius, groundLayer);
          if (colliders.Length &gt; 0)
          {
              isGrounded = true;
          }
          playerAnimator.SetBool(&quot;isJumping&quot;, !isGrounded);
      }
  }
  </code></pre>