Search Unity

2D 8 directional movement animation bug

Discussion in 'Animation' started by JoeTheHunter11, Jan 14, 2018.

  1. JoeTheHunter11

    JoeTheHunter11

    Joined:
    Sep 2, 2013
    Posts:
    3
    I'v been trying to make a top down 8 directional game for a long time but this one bug always prevents me from following through with any top down 8 directional project.

    It seems as though the player animations jump to previously played animations randomly, you can see around 10 seconds into the video the character is at a complete stop and rather than staying looking in the direction he ended in he'll spas out and face down, left, up, then down again and then stop.

    Here is the video example i recorded a while ago of this exact issue.

    I'v tried using blend trees to fix this, by calculating his x and y speed and using a blend tree to play his animations but it has the same effect, he'll spas out once you stop moving him or change this movement too fast. (The video supplied is with triggers but if you'd like to see the blend tree system I could record that for you upon request. They both have the same effect. I set up the blend tree system using this video)

    I was told by someone from the GameDev discord that my animations need exits but I don't know what that means. They said it'd be better for me to handle all animations with code rather than Unity's animator system. Still, no idea what that means.

    I'd love any piece of help I can get with this, anything at all would mean a lot to me.



    Here is the code used in the video I supplied

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8. Animator anim;
    9. public float speed = 5f;
    10. private Vector2 movement;
    11.  
    12. bool isDiagonal = false;
    13. public float delay = 0.1f;
    14.  
    15. // Use this for initialization
    16. void Start()
    17. {
    18. anim = GetComponent<Animator>();
    19. //GetComponent<Rigidbody2D>().velocity = movement;
    20. }
    21.  
    22. // Update is called once per frame
    23. void FixedUpdate()
    24. {
    25. float inputX = Input.GetAxisRaw("Horizontal");
    26. float inputY = Input.GetAxisRaw("Vertical");
    27. movement = new Vector2(inputX, inputY);
    28.  
    29. //Diagonals
    30.  
    31. if (inputX != 0 && inputY != 0)
    32. {
    33. isDiagonal = true;
    34. if (movement.y == 1 && movement.x == -1)
    35. {
    36. anim.SetTrigger("move_up_left");
    37. }
    38.  
    39. if (movement.y == 1 && movement.x == 1)
    40. {
    41. anim.SetTrigger("move_up_right");
    42. }
    43.  
    44. if (movement.y == -1 && movement.x == -1)
    45. {
    46. anim.SetTrigger("move_down_left");
    47. }
    48.  
    49. if (movement.y == -1 && movement.x == 1)
    50. {
    51. anim.SetTrigger("move_down_right");
    52. }
    53. }
    54. else
    55. {
    56.  
    57. if (isDiagonal)
    58. {
    59. StartCoroutine(NoMoreDiagonal());
    60. }
    61. else
    62. {
    63.  
    64. //left/right/up/down
    65. if (movement.x == -1)
    66. {
    67. anim.SetTrigger("move_left");
    68. }
    69.  
    70. if (movement.x == 1)
    71. {
    72. anim.SetTrigger("move_right");
    73. }
    74.  
    75.  
    76. if (movement.y == 1)
    77. {
    78. anim.SetTrigger("move_up");
    79. }
    80.  
    81.  
    82. if (movement.y == -1)
    83. {
    84. anim.SetTrigger("move_down");
    85. }
    86. }
    87. }
    88.  
    89. transform.Translate(movement * speed * Time.deltaTime);
    90. }
    91.  
    92. IEnumerator NoMoreDiagonal()
    93. {
    94. yield return new WaitForSeconds(delay);
    95. isDiagonal = false;
    96. }
    97.  
    98. }
    99.  
    100.  
    101.  
     
unityunity