Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Feedback Animation Jump does not stop playing after pressed once

Discussion in 'Animation' started by bigboss525, Oct 1, 2022.

  1. bigboss525

    bigboss525

    Joined:
    Jul 14, 2021
    Posts:
    1
    Here is my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.     [SerializeField] float runSpeed = 10f;
    9.     [SerializeField] float slideSpeed = 5f;
    10.     [SerializeField] float jumpSpeed = 5f;  
    11.     Vector2 movement;
    12.     Rigidbody2D rb;
    13.     Animator myAnimator;
    14.     BoxCollider2D myBoxCollider;
    15.  
    16.     void Start()
    17.     {
    18.         rb = GetComponent<Rigidbody2D>();
    19.         myAnimator = GetComponent<Animator>();
    20.         myBoxCollider = GetComponent<BoxCollider2D>();
    21.     }
    22.  
    23.     void Update()
    24.     {
    25.         Run();
    26.         FlipSprite();            
    27.     }
    28.    
    29.  
    30.     void FlipSprite()
    31.     {
    32.         bool playerHasHorizontalSpeed = Mathf.Abs(rb.velocity.x) > Mathf.Epsilon;
    33.  
    34.         if (playerHasHorizontalSpeed)
    35.         {
    36.             transform.localScale = new Vector2(Mathf.Sign(rb.velocity.x), 1f);
    37.         }
    38.        
    39.     }
    40.     void OnJump(InputValue value)
    41.     {      
    42.         bool isTouchingGround = myBoxCollider.IsTouchingLayers(LayerMask.GetMask("Ground"));
    43.  
    44.         if (!isTouchingGround)
    45.         {          
    46.             return;          
    47.         }
    48.  
    49.         if (value.isPressed)
    50.         {
    51.             rb.velocity += new Vector2(0f, jumpSpeed);  
    52.             myAnimator.SetBool("isJumping", true);                    
    53.         }      
    54.     }
    55.  
    56.     void Run()
    57.     {
    58.         Vector2 playerVelocity = new Vector2 (movement.x * runSpeed, rb.velocity.y);
    59.         rb.velocity = playerVelocity;
    60.  
    61.         bool playerHasHorizontalSpeed = Mathf.Abs(rb.velocity.x) > Mathf.Epsilon;      
    62.            
    63.        
    64.         myAnimator.SetBool("isRunning", playerHasHorizontalSpeed);
    65.        
    66.     }
    67.  
    68.     void OnSlide(InputValue value)
    69.     {
    70.         if (value.isPressed)
    71.         {  
    72.             rb.velocity += new Vector2(slideSpeed, 0f);
    73.             myAnimator.SetBool("isSliding", true);
    74.         }                
    75.     }
    76.  
    77.     void OnMove(InputValue value)
    78.     {
    79.         movement = value.Get<Vector2>();      
    80.     }  
    81. }
    82.  
    the result for that is this: jumpresult.PNG

    the animation then stays in this loop and doesn't change back to idle when I clearing have "isJumping" set to false
     
  2. Nathanieljla

    Nathanieljla

    Joined:
    Apr 18, 2014
    Posts:
    97
    What are you settings for the transition back to idle? Does it have an exit time? Are there any conditions that aren't being met? IF you have an exit time, AND the transition from ldle to jump takes you past this point AND the jump animation doesn't loop, then I could see the animation getting stuck. You could try adding a interrupt to the transition going from idle to jump to see if that fixes the issue.

    interrupt.PNG

    You might want to have a looping "In Air" animation that the jump animation transitions to by the end of the jump action. The air node then transitions back to idle when IsJumping is false. I actually have things like "jump" as a trigger, and another param like IsInAir, but no need to complicate things if the game doesn't need it.