How can I flip an animation direction with the player?

(I’ve only been learning unity for about 5 days now so bare with me) I’m trying to get my animation to ‘swing’ a weapon in the direction the character is facing. I’m using the flipX bool of the sprite renderer component to flip my character but that doesn’t do anything to the animation. I’ve tried using the following methods

1. anim.transform.Rotate(new Vector3(0,180,0)); //Constantly flips the world back and forth
2. anim.transform.rotation = Quaternion.Euler(0,180,0); // Flips the world but still faces left

I’ve tried to just flip the character with the above methods to no avail. The animation is a component on the player and I’ve even tried making an empty game object attached to the player that hold the animation and flipping that object to no avail as well.
I’m at a loss everyone, I can’t figure this out and I’ve tried every answer I’ve found while researching this. (I’ve been researching for hours now)

Just to first clarify for you what those two code samples mean:

The first one uses the function Rotate, which will rotate the transform BY the amount you give it. That’s why it keeps rotating back and forth, each time it rotates another 180 degrees. The second one SETS the rotation explicitly to the rotation you give it. So it will always be 180 on the Y axis.

There’s also a shorthand you can use to set eulerAngles:
transform.eulerAngles = new Vector3(0, 180, 0);
or to go further, also using the shorthand for (0,1,0) and multiplying by 180:
transform.eulerAngles = Vector3.up * 180;

If you rotate the object that the animator component lives on, the animation will play and set the transform’s rotation back to whatever it has set for rotation. It will play the animation and override your change while it plays.

However, that animation plays in local space, relative to the parent object. If there’s no parent, then it’s relative to the world origin.

So your animation can be flipped by rotating any parent transform by 180 degrees, not the animated transform itself, because the animator component controls that rotation value while the animation is playing.

3 Likes
void Flip()
{
   // Switch the way the player is labelled as facing
   facingRight = !facingRight;
   // Multiply the player's x local scale by -1
   Vector3 theScale = transform.localScale;
   theScale.x *= -1;
   transform.localScale = theScale;
}
2 Likes

Thanks for the explanation, makes sense why it didn’t work before lol

This worked, thank you!
EDIT: I should have done more testing before saying it worked lol. If I put the call to Flip in an if statement inside the check for a button press (In the update) then it flips back and forth constantly and you have to time it right to get the character to face the other way.

don’t call the flip every time, try to check if this should be executed

//isMoving,depended on your moving script

if (//isMovingLeft && facingRight)
flip();

if (//isMovingRight && !facingRight)
flip();
1 Like

I was pretty tired this morning so I didn’t even think about it but I got it just before I came on the forum, Thank you!
Everyone on this forum is so helpful, it’s absolutely amazing
Question though, Should I be making new posts for other questions or would that be considered cluttering the forum?

1 Like

Sorry, no idea :roll_eyes:
I would say, for other question create a new post. I think, most of us don’t read all posts here and just are looking at the description first. And something like "Please, how I can do this ???!!!" has low chance to be answered. And not all will read the post if the post got already many answers.

1 Like