Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to play my jump animation?

Discussion in 'Animation' started by thegamer539, Nov 5, 2020.

  1. thegamer539

    thegamer539

    Joined:
    Apr 5, 2018
    Posts:
    10
    So I wanna play my jump animation when the player is the air and when he presses space ONCE. It may sound simple but I can't figure out how to do this at all. Here's my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEditor;
    4. using UnityEngine;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.     //private variables
    9.     private Rigidbody2D rb;
    10.     bool IsGrounded = false;
    11.  
    12.     //public variables
    13.     public float speed;
    14.     public float jumpForce;
    15.     public Transform GroundChecker;
    16.     public float GroundRadius;
    17.     public LayerMask GroundLayer;
    18.     public float fallMultiplier = 2.5f;
    19.     public float lowJumpMultiplier = 2f;
    20.     public SpriteRenderer playerSprite;
    21.     public Animator playerAnimator;
    22.  
    23.  
    24.  
    25.     void Start()
    26.     {
    27.         Input.GetAxisRaw("Horizontal");
    28.         rb = GetComponent<Rigidbody2D>();
    29.     }
    30.  
    31.     void Update()
    32.     {
    33.         Move();
    34.         Jump();
    35.         CheckForGround();
    36.         BetterJump();
    37.         AnimateSprite();
    38.  
    39.     }
    40.  
    41.     void Move()
    42.     {
    43.         float xHor = Input.GetAxisRaw("Horizontal");
    44.         float MoveDir = xHor * speed;
    45.         rb.velocity = new Vector2(MoveDir, rb.velocity.y);
    46.     }
    47.  
    48.     void Jump()
    49.     {
    50.         if (Input.GetKeyDown(KeyCode.Space) && IsGrounded)
    51.         {
    52.             rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    53.         }
    54.     }
    55.  
    56.     void CheckForGround()
    57.     {
    58.         Collider2D collider = Physics2D.OverlapCircle(GroundChecker.position, GroundRadius, GroundLayer);
    59.  
    60.         if (collider != null)
    61.         {
    62.             IsGrounded = true;
    63.         }
    64.         else
    65.         {
    66.             IsGrounded = false;
    67.         }
    68.     }
    69.  
    70.     void BetterJump()
    71.     {
    72.         if (rb.velocity.y < 0)
    73.         {
    74.             rb.velocity += Vector2.up * Physics2D.gravity * (fallMultiplier - 1) * Time.deltaTime;
    75.         }
    76.         else if (rb.velocity.y > 0 && !Input.GetKey(KeyCode.Space))
    77.         {
    78.             rb.velocity += Vector2.up * Physics2D.gravity * (lowJumpMultiplier - 1) * Time.deltaTime;
    79.         }
    80.     }
    81.  
    82.     void AnimateSprite()
    83.     {
    84.         if (Input.GetKey(KeyCode.A))
    85.         {
    86.             playerSprite.flipX = true;
    87.             playerAnimator.Play("Run");
    88.         }
    89.         else if (Input.GetKey(KeyCode.D))
    90.         {
    91.             playerSprite.flipX = false;
    92.             playerAnimator.Play("Run");
    93.         }
    94.         else
    95.         {
    96.             playerAnimator.Play("Idle");
    97.         }
    98.  
    99.        
    100.      
    101.     }
    102. }
    103.  

    Please bear with my messy code.
     
  2. darsac

    darsac

    Joined:
    Aug 24, 2018
    Posts:
    34
    Code (CSharp):
    1.  
    2.  
    3. void AnimateSprite()
    4.   {
    5.  
    6.       if(!isGrounded)
    7.        {
    8.               //Play anim is in the air
    9.        }
    10.        else
    11.        {
    12.              //Stop the anim
    13.        }
    14.        if (Input.GetKey(KeyCode.A))
    15.        {
    16.           playerSprite.flipX = true;
    17.             playerAnimator.Play("Run");
    18.        }
    19.         else if (Input.GetKey(KeyCode.D))
    20.         {
    21.            playerSprite.flipX = false;
    22.            playerAnimator.Play("Run");
    23.        }
    24.         else
    25.        {
    26.            playerAnimator.Play("Idle");
    27.         }
    28.     }
    29.  
    30.  
    Or you can use a trigger statement in your animator
     
  3. thegamer539

    thegamer539

    Joined:
    Apr 5, 2018
    Posts:
    10


    Cool this works. Thanks!. although I have a problem. How do I stop an animation? The jump animation stays on if I move as soon as I hit the floor after jumping. :/
     
  4. darsac

    darsac

    Joined:
    Aug 24, 2018
    Posts:
    34
    In your animator panel you can set a trigger. When you active a trigger it became true for one cycle of animation . You have to make the transition between jump an idle state