Search Unity

How to animate transitions before flipping?

Discussion in '2D' started by rogerguimar, Jun 21, 2019.

  1. rogerguimar

    rogerguimar

    Joined:
    May 10, 2019
    Posts:
    5
    Hello, I've learned how to incorporate all the basic animation movements to a 2D plataform character but I'm struggling when it comes to transition animations that are supposed to activate before flipping the sprites horizontally.
    Basically, I need to make the character stop for as long as the transition lasts (time of the animation), play the right transition clip ("turn left" or "turn right") and then finally flip the sprites accordingly to allow normal movement again.
    Any tips on how I can achieve this?

    Thanks for the attention.
     
  2. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    This probably isn't the best way, but maybe you can use some waitforseconds in IEnumerators?
     
  3. rogerguimar

    rogerguimar

    Joined:
    May 10, 2019
    Posts:
    5
    Somehow, after using IEnumerator, and especifying the exact duration of the animation (WaitForSeconds) before allowing for the activation of Flip(), the character's movements became a mess, with the turning animation constantly flicking even when maintaining the movement in the same direction (normal walking is deactivated somehow).
    Without IEnumerator, the turn animation becomes messy (because Flip() affects it as well, I guess) but the rest of movements remain perfectly fine.
     
  4. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Are you trying to play multiple animations at once?
     
  5. rogerguimar

    rogerguimar

    Joined:
    May 10, 2019
    Posts:
    5
    I don't think so. My intention is for my character to be able to play walk or idle animation then, after pressing axis in the opposite direction, to play "turnright" or "turnleft" animation while standing still and then finally resume normal animation by flipping the sprites for walk or idle in the currently pointed direction. Example: character walks right, stops and turns around to the left for half a second, then walks left.
    Not essential stuff (works perfectly with just flipping sprites horizontally) but I really would like to polish a little my game.
     
  6. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Can you show images/videos? I dont quite understand the problem here
     
  7. rogerguimar

    rogerguimar

    Joined:
    May 10, 2019
    Posts:
    5
    Does the script help? I guess part of the problem is that I set the rigidbody speed as the factor for determining animation while I expect the turning around animation to be zero as well (same as the idle) but even if I keep the character moving the turning around animation and the walking animation keep flickering between them.

    Code (CSharp):
    1. private void FixedUpdate()
    2.     {
    3.         float h = Input.GetAxisRaw("Horizontal");
    4.            
    5.         rb.velocity = new Vector2(h * speed, rb.velocity.y);
    6.  
    7.         if (onGround)
    8.             anim.SetFloat("Speed", Mathf.Abs(rb.velocity.magnitude));
    9.  
    10.         if (h > 0 && !facingRight)
    11.         {
    12.             ZeroSpeed();
    13.             anim.SetTrigger("TurnRight");
    14.             StartCoroutine(WaitToFlip());
    15.  
    16.  
    17.         }
    18.         else if (h < 0 && facingRight)
    19.         {
    20.         ZeroSpeed();
    21.             anim.SetTrigger("TurnLeft");
    22.             StartCoroutine(WaitToFlip());
    23.  
    24.         }
    25.  
    26.     }
    27.  
    28.  
    29.     void Flip()
    30.     {
    31.         facingRight = !facingRight;
    32.         Vector3 scale = transform.localScale;
    33.         scale.x *= -1;
    34.         transform.localScale = scale;
    35.  
    36.     }
    37.     void ZeroSpeed()
    38.     {
    39.         speed = 0;
    40.     }
    41.     void ResetSpeed()
    42.     {
    43.         speed = maxSpeed;
    44.     }
    45.  
    46.     IEnumerator WaitToFlip()
    47.     {
    48.         yield return new WaitForSeconds(0.625f);
    49.         Flip();
    50.     ResetSpeed();
    51.     }
    52.  
     
  8. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Perhaps make a bool called playing animation, and if that bool is true dont play the normal walk and when it is false you can play all your animations
     
  9. rogerguimar

    rogerguimar

    Joined:
    May 10, 2019
    Posts:
    5
    I will see what I can do with that, thanks.
     
  10. sjpalacio

    sjpalacio

    Joined:
    Nov 1, 2019
    Posts:
    2
    Hello everyone, I know that the post is old, but I am trying to do and apply that same logic in my game. Style the game Unto the end ... could you solve it rogerguimar? Thanks a lot! :):)
     
  11. dusteff

    dusteff

    Joined:
    Jan 7, 2022
    Posts:
    4
    I wouldn't use enumerators.. I would have a trigger start the left/right animation, and in the animation for turn right or left have a key frame at the end which runs the flip code.
     
  12. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    Old thread, but it popped up again due to a necro and might be hit off Google, so;

    The way to solve this is to have the turn animation be flipped. So if all your animations have the character facing right, your turn animation has the character turning from left to right. Then, when you want to turn, you both play that animation and immediately flip x.

    This saves you all the problems around having to wait and timing coroutines or whatever. There's a lot of corner case bugs of setting flipx one frame too late or early that you just completely bypass with this solution.