I’ve got a quick question regarding 2D Sprite animations that I haven’t been able to find specifically answered anywhere:
I have a sprite with walk animations to the right. However, I obviously want to flip the animation to the left when he walks left (2D side-scroller).
I can easily flip the sprite itself, using transform.localscale.x
, however, that only flips the sprite. Not the animation clip. (This no longer happens in Unity)
So, while the sprite flips, the minute the animation clip begins playing, it flips back right (as the only animation clip I have is for the right facing sprite).
Is the only way to do this to flip the sprites in Photoshop, or is there a way to do it in Unity?
Thanks!
UPDATE: With the actual versions of unity if you scale the transform by multiplying it by -1
, the animation frames are also scaled.
5
Answers
I finally figured it out by doing this:
This is from Unity's 2D Platformer example.
To implement some sort of checking which makes use of the
Flip
method, you can do something similar to the below example which is basic movement code.facingRight
is set as a value on the class so that the other methods can use it, and it is defaulted tofalse
.You could also do that on transform itself (without animator). But in that case rotation value can be overriden by animator
This is how I did it – almost the same as the other technique by Jestus with unity script.
If you’re animating in Unity:
Now it should play facing the other direction!
This is my C# implementation. It uses a string as the direction facing to make it a little easier to debug.