Moving Sprite 2D Animations to Unity 2D

I have a quick question regarding Sprite 2D animation that I couldn’t find anywhere specifically for the answer:

I have a sprite with walking animation on the right. However, I obviously want to flip the animation to the left when it goes to the left (2D side scroller).

I can easily flip the sprite using transform.localscale.x , however, which only flips the sprite. Not an animation clip. (This no longer happens in Unity)

Thus, while the sprite is flipped, the moment the animation clip starts playing, it flips back to the right (since I only have one animation clip designed for the right sprite).

Is this the only way to do this to flip sprites in Photoshop, or is there a way to do this in Unity?

Thanks!

UPDATE: With the actual versions of the unit, if you scale the transformation by multiplying it by -1 , the animation frames will also scale.

+10
c # animation 2d unity3d sprite
source share
5 answers

I finally figured this out by doing the following:

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

This is from the Unity 2D Platformer example.

To implement some validation that uses the Flip method, you can do something similar to the example below, which is the base motion code. facingRight set as the value for the class, so other methods can use it, and by default it is 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")); } 
+11
source share
 void FlipHorizontal() { animator.transform.Rotate(0, 180, 0); } 

You can also do this on the conversion itself (without an animator). But in this case, the rotation value can be overridden by the animator

+2
source share

This is how I did it - almost the same as another Jest method with a script unit.

 var facing : String = "right"; function updateFacing(curr : String){ if(curr != facing){ facing = curr; var theScale : Vector3 = gameObject.transform.localScale; theScale.x *= -1; gameObject.transform.localScale = theScale; } } //put to use function controls(){ if(Input.GetKey (KeyCode.LeftArrow)){ updateFacing("left"); } else if(Input.GetKey (KeyCode.RightArrow)){ updateFacing("right"); } } 
0
source share

If you are animating in Unity:

  • Copy all frames (sprites) of the animation that you want to flip.
  • Paste these frames into your new animation and select everything in the first frame.
  • Scale x of the first frame from 1 to -1.
  • Do the same with the very last frame of your animation.

Now he must play facing a different direction!

0
source share

This is my implementation in C #. It uses a string as a direction to make it a little easier to debug.

 public string facing = "right"; public string previousFacing; private void Awake() { previousFacing = facing; } void Update() { // store movement from horizontal axis of controller Vector2 move = Vector2.zero; move.x = Input.GetAxis("Horizontal"); // call function DetermineFacing(move); } // determine direction of character void DetermineFacing(Vector2 move) { if (move.x < -0.01f) { facing = "left"; } else if (move.x > 0.01f) { facing = "right"; } // if there is a change in direction if (previousFacing != facing) { // update direction previousFacing = facing; // change transform gameObject.transform.Rotate(0, 180, 0); } } 
0
source share

All Articles