- Home /
LocalScale not working idealy
Hi, so I just want a basic code which makes my enemies flip when they face a new direction. I was messing around with this here and got it to flip, but when it flipped, it got unproportional. If you could help me I'd greatly appreciate it!
This is the game when they are weirdly messed up:
And when they are normal:
Here's the code for it:
public bool FacingRight = true;
public float rightSize = -1f;
public float leftSize = 1f;
void Update ()
{
if (transform.rotation.eulerAngles.z > 90 && transform.rotation.eulerAngles.z < 270 && FacingRight == true) {
transform.localScale += new Vector3 (0, leftSize, 0);
FacingRight = false;
}
if (transform.rotation.eulerAngles.z < 90 && FacingRight == false) {
transform.localScale += new Vector3 (0, rightSize, 0);
FacingRight = true;
}
if (transform.rotation.eulerAngles.z > 270 && FacingRight == false) {
transform.localScale += new Vector3 (0, rightSize, 0);
FacingRight = true;
}
}
Thanks!
Answer by maccabbe · Feb 24, 2015 at 02:37 AM
You are adding to the localscale when you do
transform.localScale += new Vector3 (0, leftSize, 0).
Instead, if you want to flip the scale you probably want to assign it a value like
transform.localScale=new Vector3(xScale, yScale, zScale)
If you want to flip the y scale then you would do
transform.localScale=new Vector3(transform.localScale.x, -transform.localScale.y, transform.localScale.z)
Your answer
Follow this Question
Related Questions
Roguelike Tutorial Expansion of Layout 1 Answer
2D Box Collider doesn't flip with Sprite 3 Answers
How to do a sprite flip 1 Answer
Multiple Cars not working 1 Answer
My sprite is teleporting when I change my scale to -1 to flip it 1 Answer