Search Unity

i am Beginner and i Need help with Jumping Animation please

Discussion in '2D' started by BraniDev, Nov 11, 2019.

  1. BraniDev

    BraniDev

    Joined:
    Aug 3, 2019
    Posts:
    25
    i have Bug when i play the game Walking and Idle animation is totally fine but when i jump .. Jump animation play and after character is not doing any animation at all
    my Player script

    https://pastebin.com/ZNsS1Mv5
    photo of my Unity here https://imgur.com/a/NINz8Pz

    i hope somebody will help me already i lost 2 days working on this code changing him few time i am newbie at Unity
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @Maniak147

    Hint: to maximize probability to get an answer, it is best to attach both the images and code here permanently.

    You can use code tags to format your code in your post and use Upload a File button next to Post Reply to attach images.
     
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    vakabaka likes this.
  4. BraniDev

    BraniDev

    Joined:
    Aug 3, 2019
    Posts:
    25
    i try to edit it but forum says i am a spammer i cannot edit the thread anymore ... next time i will do it correct
    can you help me with my issue?
     
  5. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (_grounded == true)
    4.         {
    5.             extraJumps = extraJumpsValue;
    6.             m_Jump = true;
    7.  
    8. //check this for test and say what will be wrong here
    9. m_Animator.SetBool("Jumping", false);
    10.         }
     
  6. BraniDev

    BraniDev

    Joined:
    Aug 3, 2019
    Posts:
    25
    great
    when i jump is not playing animation but when i double jump it is playing normally and it come back to walk animation
    so is good 50% is only playing when i double jumping (pressing 2x space)
    so just fix this and i am good to go
     
  7. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    ok, here is the delay between fixed update and update. The bool is switching back to false in the time as the player is flying up (because your circle check moving over the ground in not only one frame. I will think bit, how to prevent this.

    ps. the simplest way were to add some time to prevent that the bool going false in next time

    pps. or you can allow to change the bool if the players speed was negative.
     
  8. BraniDev

    BraniDev

    Joined:
    Aug 3, 2019
    Posts:
    25
    but i just add your code i didnt delete anything what i have in my code... btw you are kida superhero to me now... thanks
     
  9. BraniDev

    BraniDev

    Joined:
    Aug 3, 2019
    Posts:
    25
    i have no idea what to do can you write to code?
     
  10. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    not sure if this will work, my scripts work almost never on first try :)

    ps. I have edited it bit

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6.  
    7. public class Player : MonoBehaviour
    8. {
    9.  
    10.     [SerializeField]
    11.     public float speed;
    12.     private float moveInput;
    13.  
    14.     private Rigidbody2D rb;
    15.     [SerializeField]
    16.     public float jumpForce;
    17.  
    18.     private bool facingRight = true;
    19.  
    20.     private bool _resetJump = false;
    21.     private bool _grounded = false;
    22.  
    23.  
    24.     public Transform groundCheck;
    25.     public float checkRadius;
    26.     public LayerMask whatIsGround;
    27.  
    28.     private int extraJumps;
    29.     public int extraJumpsValue;
    30.  
    31.     private PlayerAnimation _playerAnim;
    32.     private SpriteRenderer _playerSprite;
    33.     Animator m_Animator;
    34.     bool m_Jump;
    35.  
    36.     //here new bool
    37.     bool hasJumped = false;
    38.  
    39.  
    40.     void Start()
    41.     {
    42.         extraJumps = extraJumpsValue;
    43.         rb = GetComponent<Rigidbody2D>();
    44.         _playerAnim = GetComponent<PlayerAnimation>();
    45.         _playerSprite = GetComponentInChildren<SpriteRenderer>();
    46.         m_Animator = GetComponentInChildren<Animator>();
    47.         m_Jump = false;
    48.  
    49.     }
    50.  
    51.     void FixedUpdate()
    52.     {
    53.         _grounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
    54.  
    55.  
    56.  
    57.         moveInput = Input.GetAxis("Horizontal");
    58.  
    59.         rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
    60.  
    61.         if (facingRight == false && moveInput > 0)
    62.         {
    63.             Flip();
    64.         }
    65.         else if (facingRight == true && moveInput < 0)
    66.         {
    67.             Flip();
    68.         }
    69.         _playerAnim.Move(moveInput);
    70.  
    71.  
    72.     }
    73.  
    74.  
    75.  
    76.  
    77.     void Update()
    78.     {
    79.  
    80.         //here check if the player is falling
    81.         if (hasJumped && rb.velocity.y < 0)
    82.         {
    83.             hasJumped = false;
    84.         }
    85.  
    86.         if (_grounded == true)
    87.         {
    88.             extraJumps = extraJumpsValue;
    89.             m_Jump = true;
    90.  
    91.             //here allow the end the animation if the player is falling and the grounded is true
    92.             if (!hasJumped && m_Animator.GetBool("Jumping"))
    93.             {
    94.                 m_Animator.SetBool("Jumping", false);
    95.             }
    96.  
    97.         }
    98.  
    99.  
    100.         if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
    101.         {
    102.             rb.velocity = Vector2.up * jumpForce;
    103.             extraJumps--;
    104.  
    105.  
    106.             if (m_Jump == true)
    107.                 m_Animator.SetBool("Jumping", true);
    108.             Debug.Log("playing jumping animation");
    109.  
    110.  
    111.             //here your player has began the jump
    112.             hasJumped = true;
    113.  
    114.  
    115.  
    116.         }
    117.         // i think here somewhere is the bug...
    118.         else if (Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && _grounded == true)
    119.         {
    120.             rb.velocity = Vector2.up * jumpForce;
    121.             rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    122.             m_Animator.SetBool("Jumping", false);
    123.             Debug.Log("I am Jumping");
    124.  
    125.  
    126.         }
    127.  
    128.  
    129.     }
    130.  
    131.  
    132.     void Flip()
    133.     {
    134.         facingRight = !facingRight;
    135.         Vector3 Scaler = transform.localScale;
    136.         Scaler.x *= -1;
    137.         transform.localScale = Scaler;
    138.  
    139.     }
    140.  
    141.  
    142.  
    143. }
    144.  
     
    Last edited: Nov 11, 2019
  11. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    here with wait time
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class Player : MonoBehaviour
    7. {
    8.  
    9.     [SerializeField]
    10.     public float speed;
    11.     private float moveInput;
    12.  
    13.     private Rigidbody2D rb;
    14.     [SerializeField]
    15.     public float jumpForce;
    16.  
    17.     private bool facingRight = true;
    18.  
    19.     private bool _resetJump = false;
    20.     private bool _grounded = false;
    21.  
    22.  
    23.     public Transform groundCheck;
    24.     public float checkRadius;
    25.     public LayerMask whatIsGround;
    26.  
    27.     private int extraJumps;
    28.     public int extraJumpsValue;
    29.  
    30.     private PlayerAnimation _playerAnim;
    31.     private SpriteRenderer _playerSprite;
    32.     Animator m_Animator;
    33.     bool m_Jump;
    34.  
    35.     //here new bool
    36.     bool hasJumped = false;
    37.  
    38.  
    39.     void Start()
    40.     {
    41.         extraJumps = extraJumpsValue;
    42.         rb = GetComponent<Rigidbody2D>();
    43.         _playerAnim = GetComponent<PlayerAnimation>();
    44.         _playerSprite = GetComponentInChildren<SpriteRenderer>();
    45.         m_Animator = GetComponentInChildren<Animator>();
    46.         m_Jump = false;
    47.  
    48.     }
    49.  
    50.     void FixedUpdate()
    51.     {
    52.         _grounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
    53.  
    54.  
    55.  
    56.         moveInput = Input.GetAxis("Horizontal");
    57.  
    58.         rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
    59.  
    60.         if (facingRight == false && moveInput > 0)
    61.         {
    62.             Flip();
    63.         }
    64.         else if (facingRight == true && moveInput < 0)
    65.         {
    66.             Flip();
    67.         }
    68.         _playerAnim.Move(moveInput);
    69.  
    70.  
    71.     }
    72.  
    73.  
    74.  
    75.  
    76.     void Update()
    77.     {
    78.         if (_grounded == true)
    79.         {
    80.             extraJumps = extraJumpsValue;
    81.             m_Jump = true;
    82.  
    83.             //here allow to end the animation if some time was passed
    84.             if (!hasJumped && m_Animator.GetBool("Jumping"))
    85.             {
    86.                 m_Animator.SetBool("Jumping", false);
    87.             }
    88.  
    89.         }
    90.  
    91.  
    92.         if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
    93.         {
    94.             rb.velocity = Vector2.up * jumpForce;
    95.             extraJumps--;
    96.  
    97.  
    98.             if (m_Jump == true)
    99.                 m_Animator.SetBool("Jumping", true);
    100.             Debug.Log("playing jumping animation");
    101.  
    102.  
    103.             //here your player has began the jump
    104.             if (!hasJumped)
    105.             {
    106.                 hasJumped = true;
    107.                 StartCoroutine("WaitBitAfterJump");
    108.             }
    109.  
    110.  
    111.  
    112.         }
    113.         // i think here somewhere is the bug...
    114.         else if (Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && _grounded == true)
    115.         {
    116.             rb.velocity = Vector2.up * jumpForce;
    117.             rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    118.             m_Animator.SetBool("Jumping", false);
    119.             Debug.Log("I am Jumping");
    120.  
    121.  
    122.         }
    123.  
    124.  
    125.     }
    126.  
    127.  
    128.     void Flip()
    129.     {
    130.         facingRight = !facingRight;
    131.         Vector3 Scaler = transform.localScale;
    132.         Scaler.x *= -1;
    133.         transform.localScale = Scaler;
    134.  
    135.     }
    136.  
    137.     //here new coroutine
    138.     //wait for 1f seconds and change the bool
    139.     //you can change it to lower value 0.5f
    140.     IEnumerator WaitBitAfterJump ()
    141.     {
    142.         yield return new WaitForSeconds(1f);
    143.         hasJumped = false;
    144.     }
    145.  
    146. }
     
    Last edited: Nov 11, 2019
  12. BraniDev

    BraniDev

    Joined:
    Aug 3, 2019
    Posts:
    25
    thank you again i should have a contant on you maybe next if i need help :)