Search Unity

Bug with animator (apparently with transitions)

Discussion in 'Animation' started by Vaspix, Jan 18, 2020.

  1. Vaspix

    Vaspix

    Joined:
    Jun 8, 2019
    Posts:
    54
    Hi, I was testing my 2D movement and faced a problem:
    When the player goes and jumps in one direction, then changes the direction in midair and keeps going, when it falls to the ground, it looks in the opposite (1st) direction for 1 frame, after that the animation starts and everything work as it should.
    All that stuff happens in this statement:
    Code (CSharp):
    1. anim.enabled = true;
    2.  
    3.                 if (Move == 1)
    4.                     anim.SetInteger("AnimMode", 1);
    5.                 else if (Move == -1)
    6.                     anim.SetInteger("AnimMode", 2);
    I have a transition to empty Idle box in Animator from start and 2 transitions each from Any state to the WalkRight and WalkLeft boxes.

    I don't know how to describe this problem properly but if you have any thoughts on what should I check in my code / animator or assume what could cause that type of a problem I'll be happy to hear that.
     
  2. Vaspix

    Vaspix

    Joined:
    Jun 8, 2019
    Posts:
    54
    This is the full code of my Player movement if you don't mind taking your time to find the issue
    Code (CSharp):
    1. Rigidbody2D rb;
    2.     GroundCollider grColl;
    3.     public float runSpeed = 5f, jHeight = 330f;
    4.     float Move;
    5.     bool Jump;
    6.  
    7.     Animator anim;
    8.     SpriteRenderer rend;
    9.     [SerializeField] Sprite Right1, Right2, Left1, Left2, rJump1, rJump2, lJump1, lJump2;
    10.     float lastPosY;
    11.     bool looksRight = true;
    12.  
    13.     GroundCollider leftColl, rightColl;
    14.  
    15.     void Start()
    16.     {
    17.         rb = GetComponent<Rigidbody2D>();
    18.         grColl = GameObject.Find("GroundCollider").GetComponent<GroundCollider>();
    19.  
    20.         rend = GetComponent<SpriteRenderer>();
    21.         anim = GetComponent<Animator>();
    22.         lastPosY = transform.position.y;
    23.  
    24.         leftColl = GameObject.Find("LeftCollider").GetComponent<GroundCollider>();
    25.         rightColl = GameObject.Find("RightCollider").GetComponent<GroundCollider>();
    26.  
    27.         Physics2D.IgnoreLayerCollision(8, 9);
    28.     }
    29.     void Update()
    30.     {
    31.         // Check for Move, change Sprite
    32.         if (Input.GetKey(KeyCode.D) && !rightColl.isColl)
    33.         {
    34.             Move = 1;
    35.             looksRight = true;
    36.         }
    37.         else if (Input.GetKey(KeyCode.A) && !leftColl.isColl)
    38.         {
    39.             Move = -1;
    40.             looksRight = false;
    41.         }
    42.         else if (Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.A) || rightColl.isColl || leftColl.isColl)
    43.             Move = 0;
    44.  
    45.         // Check for Jump
    46.         if (Input.GetKeyDown(KeyCode.W) && grColl)
    47.             Jump = true;
    48.         else if (Input.GetKeyUp(KeyCode.W))
    49.             Jump = false;
    50.  
    51.         // Animation
    52.         if (grColl.isColl)
    53.         {
    54.             /*if (looksRight) // Set Ground Sprite
    55.                 rend.sprite = Right1;
    56.             else
    57.                 rend.sprite = Left1;*/
    58.  
    59.  
    60.             if (Move == 0)
    61.                 anim.enabled = false;
    62.             else // // Bug with this statement: walk & jump in one direction, change direction in midair, when it falls it looks in the opposite (1st) direction for 1 frame, then starts walking normally
    63.             {
    64.                 anim.enabled = true;
    65.  
    66.                 if (Move == 1)
    67.                     anim.SetInteger("AnimMode", 1);
    68.                 else if (Move == -1)
    69.                     anim.SetInteger("AnimMode", 2);
    70.             } // //
    71.         }
    72.         else
    73.         {
    74.             anim.enabled = false;
    75.  
    76.             if (lastPosY + 0.01f < transform.position.y) // posY increased
    77.             {
    78.                 if (looksRight)
    79.                     rend.sprite = rJump1;
    80.                 else
    81.                     rend.sprite = lJump1;
    82.  
    83.                 lastPosY = transform.position.y;
    84.             }
    85.             else if (lastPosY - 0.01f > transform.position.y) // posY decreased
    86.             {
    87.                 if (looksRight)
    88.                     rend.sprite = rJump2;
    89.                 else
    90.                     rend.sprite = lJump2;
    91.  
    92.                 lastPosY = transform.position.y;
    93.             }
    94.         }
    95.     }
    96.  
    97.     void FixedUpdate()
    98.     {
    99.         // Move
    100.         if (Move != 0)
    101.             rb.transform.Translate(runSpeed * Move * Time.deltaTime, 0, 0);
    102.  
    103.         // Jump
    104.         if (Jump && grColl.isColl)
    105.         {
    106.             rb.Sleep();
    107.             rb.AddForce(new Vector2(0, jHeight * Time.deltaTime), ForceMode2D.Impulse);
    108.             Jump = false;
    109.         }
    110.     }