2D Sprite Flipping Problems

Hello, I’m making a 2d adventure game, and when the character flips ( changes facing direction from left to right or right to left ) , he gets squashed :S , any idea what makes this happen? or how to fix this?

An Example

Character’s body before changing directions (flip)

( )
l___l
l l

After changing directions he is facing (flip)

()
ll
l l

The animation literally halves itself, I have no idea what its causing it, any help would be greatly apprecitated!

How are you doing the flipping?

I have put a “flip” and “flip2” on a total of 2 box colliders for the walls, one on the left, and the other one is a empty game object with a 2d collider attached as a child with the wall, set on the right of the wall.

Left box collider ----> l__l <---- Right box collider, when the character touches either one of the colliders, it changes the player’s direction he is facing.

Okay, and what are you doing to make the character flip itself?

That’s the only way that he can flip, when he hits a wall, he flips in the opposite direction, you want me to post the script?

Yes please.

There you go

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
public float power =4;
public float jumpHeight =4f;

void Update()
{

transform.Translate(Vector2.right * power *Time.deltaTime);

}

void OnCollisionStay2D(Collision2D coll)
{

if (coll.gameObject.tag ==“flip”)
{

power =-4;

transform.localScale =new Vector3(-1, transform.localScale.y, transform.localScale.z);

}

if (coll.gameObject.tag ==“flip2”)
{

power =4;

transform.localScale =new Vector3(1, transform.localScale.y, transform.localScale.z);

}

if (Input.GetKeyDown (KeyCode.Space))
{

GetComponent().velocity = new Vector2 (0, jumpHeight);

}

}

}

Hmm, the flipping looks okay (setting X scale to -1 to flip.) Although I’d rather recommend rotating about the Y axis to flip it around, and using transform.right instead of Vector3.right to move. That way it is not necessary to invert your “power” value because you can always move forward (=local right.)

Regarding your original problem, have you checked the transform values in the inspector at runtime after you do the flip? From your code, they should all look the same as before, with only the X scale changed to -1, of course.

You could also try something out. Start your game like normal, but don’t provoke a flip to the left. Then, pause the game, and just enter -1 into the X scale field. Does it look the same (squished) now?

Also, perhaps a screenshot (before/after) might be helpful.

Can you explain it to me a bit more in details? I’m new to unity and scripting, sorry if it sounds easy but I don’t understand.

Sure. My suggestion for using transform.right instead of Vector3.right is just that you will usually want to respect an object’s orientation. Meaning, you will want to move it into its own “forward” direction, for whatever that means for this particular object. If its “forward” direction is “up” in the world (=Vector3.up), so be it - move it into that direction, and it will indeed go up. But to the object, it will always be “forward.” This greatly simplifies your code and will hurt your brain less, because it’s usually the same for every object.

Now, to do that, you need to make sure that an object’s “forward” always points in the correct direction. If you just rotate it, this should always be true. But if you just change its scale into the negative, then only its representation will flip around, but its local axes will not. So for example a sprite that has “right”=“world right”, scaling it to X=-1 will not change its local “right” axis. Rotating it about the Y axis will.

Note that for sprites (and 2D mode in general) there is usually no “forward” direction, since that direction is usually on the Z axis which has no meaning for sprites (when using an orthographic camera, anyway.) In this case, “forward” is more of a logical direction which in reality is (local) “right.” Or “up,” depending on what you are doing.

Got it, although I am confused on how exactly to change the script and make it work without a problem, since you have way more knowledge than me in scripting, would it be possible for you to correct it for me? ( I’m scared to not do it right and make te script non functional, happend already and I had to restart all over :confused: ) Unless that is too much to ask you for, once again thank you for helping me out, you’re awesome :slight_smile:

It’s not really complicated.

  • Instead of Vector3.right, use transform.right.

  • Instead of setting localScale, you can set localRotation:

transform.localRotation *= Quaternion.Euler(0f, 180f, 0f);

The Euler() function returns a Quaternion that describes the rotation you want (180 degrees about the Y axis), while multiplying it with whatever localRotation was before will rotate it ever more. So it will rotate 180 the first time, then another 180 degrees the next time, and so on.

  • Lastly, remove the lines “power = 4” and “power = -4”, because you don’t need them any longer.

I have been working on it for awhile and I can’t seem to get it work, here’s how the script looks right now.
I am getting this error

" Assets/Scripts/PlayerController.cs(26,17): error CS0029: Cannot implicitly convert type UnityEngine.Vector3' to float’ "

using UnityEngine;
using System.Collections;
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
public float power =4;
public float jumpHeight =4f;
public float localRotation;
void Update()
{

transform.Translate(Vector2.right * power *Time.deltaTime);

}

void OnCollisionStay2D(Collision2D coll)
{

if (coll.gameObject.tag ==“flip”)
{

localRotation = transform.right;

}

if (coll.gameObject.tag ==“flip2”)
{

transform.localRotation *= Quaternion.Euler(0f, 180f, 0f);

}

if (Input.GetKeyDown (KeyCode.Space))
{

GetComponent().velocity = new Vector2 (0, jumpHeight);

}

}
}

The error is this: localRotation = transform.right;
This should read the same as the other one: transform.localRotation *= Quaternion.Euler(0f, 180f, 0f);

Also, please use CODE tags when posting source code.

Sorry, I didn’t know how to do the codes tags :o I will from now on, thank you alot for your help, I got it to work!