Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Animation changing for split second

Discussion in 'Animation' started by darthcoder567, May 4, 2023.

  1. darthcoder567

    darthcoder567

    Joined:
    Aug 20, 2022
    Posts:
    45
    I have a falling and jumping animation for my character and whenever my character reaches the peak of his jump his animation changes to the idle animation for a split second how do i fix this? this is my code:
    Code (CSharp):
    1. public class PlayerScript : MonoBehaviour
    2. {
    3.     private Rigidbody2D rb;
    4.     private SpriteRenderer sprite;
    5.     private Animator anim;
    6.  
    7.     private float dirX = 0f;
    8.     [SerializeField]private float moveSpeed = 7f;
    9.     [SerializeField]private float jumpForce = 14f;
    10.     private enum movementState {idle, running, jump, fall }
    11.  
    12.     // Start is called before the first frame update
    13.     private void Start()
    14.     {
    15.         rb = GetComponent<Rigidbody2D>();
    16.         anim = GetComponent<Animator>();
    17.         sprite = GetComponent<SpriteRenderer>();
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     private void Update()
    22.     {
    23.         dirX = Input.GetAxisRaw("Horizontal");
    24.         rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);
    25.  
    26.         if (Input.GetButtonDown("Jump"))
    27.         {
    28.             rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    29.         }
    30.  
    31.         UpdateAnimateState();
    32.     }
    33.  
    34.     private void UpdateAnimateState()
    35.     {
    36.         movementState state;
    37.  
    38.         if (dirX > 0f)
    39.         {
    40.             state = movementState.running;
    41.         }
    42.         else if (dirX < 0f)
    43.         {
    44.             state = movementState.running;
    45.         }  
    46.         else
    47.         {
    48.             state = movementState.idle;
    49.         }
    50.  
    51.         if (rb.velocity.y > .1f)
    52.         {
    53.             state = movementState.jump;
    54.         }
    55.         else if (rb.velocity.y < -.1f)
    56.         {
    57.             state = movementState.fall;
    58.         }
    59.  
    60.         anim.SetInteger("State", (int)state);
    61.  
    62.     }
    63.  
    64.  
    65.  
    66. }
     
  2. darthcoder567

    darthcoder567

    Joined:
    Aug 20, 2022
    Posts:
    45
    Also if you are moving while jumping it starts trying to play the walk animation for a split second
     
  3. arskhanFRS

    arskhanFRS

    Joined:
    May 16, 2022
    Posts:
    11
    In your animator settings try playing with exit time and transition time and see if it helps
     
  4. ElXill

    ElXill

    Joined:
    Jun 2, 2017
    Posts:
    43
    Code (CSharp):
    1.         if (rb.velocity.y > .1f)
    2.         {
    3.             state = movementState.jump;
    4.         }
    5.         else if (rb.velocity.y < -.1f)
    6.         {
    7.             state = movementState.fall;
    8.         }
    I think this is your problem, when your when your y velocity is between 0.1 and -0.1 which is at the top. Your conditions for jump states doesn't met. So your state becomes as idle or running.
     
  5. darthcoder567

    darthcoder567

    Joined:
    Aug 20, 2022
    Posts:
    45
    how do i fix that?
     
  6. ElXill

    ElXill

    Joined:
    Jun 2, 2017
    Posts:
    43
    Checking velocity of y is not a good way to find out if character is jumping. I haven't done something similar yet but I often saw an "IsGrounded" bool used for this. I'm not sure about the condition used for it, but I'm sure you can find something about this if you check the net.
     
  7. darthcoder567

    darthcoder567

    Joined:
    Aug 20, 2022
    Posts:
    45
    and how do i use the isgrounded bool in my code
     
  8. darthcoder567

    darthcoder567

    Joined:
    Aug 20, 2022
    Posts:
    45
    wherever I try to put the bool l it doesnt work
     
    Last edited: May 8, 2023
  9. darthcoder567

    darthcoder567

    Joined:
    Aug 20, 2022
    Posts:
    45
    this is my code am i supposed to use an else if or just an if statement
    1. public class PlayerScript : MonoBehaviour
    2. {
    3. private Rigidbody2D rb;
    4. private SpriteRenderer sprite;
    5. private Animator anim;

    6. private float dirX = 0f;
    7. [SerializeField]private float moveSpeed = 7f;
    8. [SerializeField]private float jumpForce = 14f;
    9. private enum movementState {idle, running, jump, fall }

    10. // Start is called before the first frame update
    11. private void Start()
    12. {
    13. rb = GetComponent<Rigidbody2D>();
    14. anim = GetComponent<Animator>();
    15. sprite = GetComponent<SpriteRenderer>();
    16. }

    17. // Update is called once per frame
    18. private void Update()
    19. {
    20. dirX = Input.GetAxisRaw("Horizontal");
    21. rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);

    22. if (Input.GetButtonDown("Jump"))
    23. {
    24. rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    25. }

    26. UpdateAnimateState();
    27. }

    28. private void UpdateAnimateState()
    29. {
    30. movementState state;

    31. if (dirX > 0f)
    32. {
    33. state = movementState.running;
    34. }
    35. else if (dirX < 0f)
    36. {
    37. state = movementState.running;
    38. }
    39. else
    40. {
    41. state = movementState.idle;
    42. }

    43. if (rb.velocity.y > .1f)
    44. {
    45. state = movementState.jump;
    46. }
    47. else if (rb.velocity.y < -.1f)
    48. {
    49. state = movementState.fall;
    50. }

    51. anim.SetInteger("State", (int)state);

    52. }



    53. }
     
  10. ElXill

    ElXill

    Joined:
    Jun 2, 2017
    Posts:
    43
    I think you are using this tutorial, I'm suprised that his code doesn't have same problem. You can go with a better and more complicated solution such as checking collision between player and terrain to see it player is grounded (I think he does this in the following videos). But in this case changing animator state transitions would probably help.

    Kill connection from jumping to running, jumping to idle, and faling to jumping. If you not gonna double jump this should be fine. In that case add faling to jumping connection back.

    Untitled.jpg
     
  11. darthcoder567

    darthcoder567

    Joined:
    Aug 20, 2022
    Posts:
    45
    This helped so much thank!:)