Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flipping a 2D Sprite Animation in Unity 2D

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.

like image 500
Jestus Avatar asked Oct 26 '14 00:10

Jestus


People also ask

How do I reverse an animation in Unity?

Timeline can't currently play animations backwards. To fix this, you can duplicate your animation in the project window, and then create a reversed copy in "Keys" view by dragging right to its end then scaling negatively, which works, but is a pain on large animations as the Animation UI becomes quite unresponsive.


2 Answers

void FlipHorizontal()
{
    animator.transform.Rotate(0, 180, 0);
}

You could also do that on transform itself (without animator). But in that case rotation value can be overriden by animator

like image 107
Deepscorn Avatar answered Oct 04 '22 08:10

Deepscorn


I finally figured it out by doing this:

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;
}

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 to false.

void Update() 
{

    //On X axis: -1f is left, 1f is right

    //Player Movement. Check for horizontal movement
    if (Input.GetAxisRaw ("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f) 
    {
        transform.Translate (new Vector3 (Input.GetAxisRaw ("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
        if (Input.GetAxisRaw ("Horizontal") > 0.5f && !facingRight) 
        {
            //If we're moving right but not facing right, flip the sprite and set     facingRight to true.
            Flip ();
            facingRight = true;
        } else if (Input.GetAxisRaw("Horizontal") < 0.5f && facingRight) 
        {
            //If we're moving left but not facing left, flip the sprite and set facingRight to false.
            Flip ();
            facingRight = false;
        }

    //If we're not moving horizontally, check for vertical movement. The "else if" stops diagonal movement. Change to "if" to allow diagonal movement.
    } else if (Input.GetAxisRaw ("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f) 
    {
        transform.Translate (new Vector3 (0f, Input.GetAxisRaw ("Vertical") * moveSpeed * Time.deltaTime, 0f));
    }

    //Variables for the animator to use as params
    anim.SetFloat ("MoveX", Input.GetAxisRaw ("Horizontal"));
    anim.SetFloat ("MoveY", Input.GetAxisRaw ("Vertical"));

}
like image 25
Jestus Avatar answered Oct 04 '22 08:10

Jestus