6
\$\begingroup\$

I'm toying around with the new Sprite class in Unity, and I'm trying to work out how to change the direction the sprite is facing ("flip" it). I'm sure I can most likely just scale it across the x axis by -1, but I'd just like to know if there's a new, "official" way of doing it with Sprite or SpriteRenderer. Thanks!

\$\endgroup\$
  • \$\begingroup\$ flip the texture \$\endgroup\$ – concept3d Dec 9 '13 at 6:19
  • \$\begingroup\$ Note that simple flipping leads to ambidextrous sprites, which may or may not be appropriate for the game you're making. If you want to avoid that, you'll either need a perfectly symmetric sprite, or a seperate (set of) sprites for the reverse direction. \$\endgroup\$ – Michael Madsen Dec 10 '13 at 10:45
8
\$\begingroup\$

Looks like the "official" way is to just invert the scale across the x axis. (That's how it's done in the 2D project example, so I think it's safe to assume there's no fancier way of doing it.)

\$\endgroup\$
4
\$\begingroup\$

You should aim to keep the scale of your "containers" like the root of the character at (1,1,1). So I recommend having the sprite as a child of the gameobject, you don't want changing the scale of the sprite affecting the collider for example. Then flip the scale of that child.

\$\endgroup\$
  • 1
    \$\begingroup\$ I second this, when scaling the x your rotations will reverse as well. If you want to flip the sprite and save yourself a lot of headache, make it a child and scale the child's x to -1. \$\endgroup\$ – Heckman Mar 7 '14 at 16:33
1
\$\begingroup\$

Like others said, you can flip the x value of the local scale:

var direction = 1; //  or -1, depending which way you want the sprite pointing.

// Sets the local scale of the current GameObject
transform.localScale = new Vector3(direction, 1, 1);

(Try flipping the y value too!)

\$\endgroup\$
  • 2
    \$\begingroup\$ You're right. Two others have already said. Why answer with the same thing again? \$\endgroup\$ – nathanchere Sep 23 '14 at 7:27
  • \$\begingroup\$ No one had any code samples, I figured it would be nice to have one. \$\endgroup\$ – John Bubriski Sep 24 '14 at 17:07
0
\$\begingroup\$

Per the Unity 4.3 - 2D Game Walkthrough at about 8:15, their implementation of the Flip() function to reverse the character sets the x scale to -1

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.