I have a simple player object colliding with ‘power ups’. I added a IncreaseSize() method that transforms the objects scale by adding 0.1f. This works fine for the y and z (even though z doesnt matter) but the x just gets all crazy. I’m not sure why? Here’s how the method is called:
So it add’s it and I get additions. The object size is 0.4,0.40.4 on default. After the first addition it becomes -0.5,0.5,0.5, and so forth. Then eventually it flips and starting adding back toward the normal direction -0.2,0.8,0.8 and then 0.1,0.9,0.9.
I’ve never worked with vector’s before unity or c#, so this is new to me. Perhaps I’m over looking something?
There is several information missing but i guess you’re doing a 2d game? Since you talk about your character “flipping” i guess you actually negate the x scale whenever you “flip” your character. Of course “adding” or subtracting to the scale won’t work that way if your character is currently flipped around. That’s why you usually multiply the scale by a scale factor. Something like
transform.localScale *= 1.02f;
Of course this would increase the scale by 2% of the current size.
If you really want to “add” to the scale which might have the x part flipped you have to do something like this:
Vector3 s = transform.localScale
transform.localScale = s + new Vector3(Mathf.Sign(s.x),0,1) * sizeIncrease;
This will grab the sign of the current scale on x and use this instead of “1”. So if the character is currently flipped Mathf.Sign(s.x) will return “-1”. So when your scale is (-0.5, 1, 0.5) and you want to add 0.2 you actually add (-0.2, 0, 0.2) and end up at (-0.7, 1, 0.7)