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!
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.
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.
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 ) Unless that is too much to ask you for, once again thank you for helping me out, you’re awesome
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.
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.