- Home /
The question is answered, right answer was accepted
Position bug on changing scale
I downloaded a simple 2d character asset from the assets store And this prefab consists of different parts with different Z position
However, when I enter to play mode, everything is fine, until I actually change its scale (programmatically): the z position of every object becomes the same:
Here is my script:
if(Input.GetKey(KeyCode.D)){
this.transform.localScale = new Vector2(-1, transform.localScale.y);
Answer by troien · Jun 06, 2019 at 09:50 AM
I think your problem is that you are setting your scale.z to 0 in that line of code. Meaning that your position z (of childs of that object) is multiplied by 0, with 0 as a result ;)
transform.localScale is a Vector3. You are giving it a Vector2. A Vector2 is implicitly converted to a Vector3. But as stated in the docs, the z component defaults to 0.
So to fix you problem, I think you want to do this instead:
this.transform.localScale = new Vector3(-1, transform.localScale.y, 1);