- Home /
Flip Y Scale Value Issue
Hi guys, i'm simply trying to flip my sprite upside down by changing the y scale value of it.
Here's the code i used that it's not working.
if(rigidbody2D.velocity.x < 0f)
{
Vector3 tempScale = transform.localScale;
tempScale.y = -1f;
transform.localScale = tempScale;
}
else if(rigidbody2D.velocity.x >= 0f)
{
Vector3 tempScale = transform.localScale;
tempScale.y = 1f;
transform.localScale = tempScale;
}
I'm sure that there's nothing in my entire solution that is overwriting the y scale value. I putted this code inside the Update function and i checked with a Debug.Log that Unity goes inside both if statements according to the x velocity of the rigidbody like i wanted, so i don't know what the error could be.
Please Help.
Sorry, I don't see why that fails. Try a "sanity check" using debug to show the transform scale after you assign to it, perhaps that will offer a clue. Any particular reason you are using a negative scale, rather than rotating it? (note: negative scale should make a mirror image, rotating it should not. Is that why?)
Try changing the scale in FixedUpdate and putting a Debug.Break(); after you call it.
This may help with debuging.
It might be that your velocity is being read as 0 at certain points, in which case the >= 0f condition will be used. As @Skeledurr said, FixedUpdate might help, but I'd also try to use a system that only changes the scale if it should change. That's a good idea anyways, now you're setting the scale every frame, when you really don't need to change it unless it's crossed from positive to negative or the other way around.
Is going in both parts regarding to your expectations? What is not working?
@Glurth I'm using a negative scale cause I need a mirror image of my sprite. Anyway thanks a lot guys for the quick answers, i will try all of your advices.
Answer by george_vasilchenko · Jan 08, 2015 at 10:48 AM
Hey, everything works, just put
rigidbody2D.velocity.x <= 0f in the first if statement instead of just
rigidbody2D.velocity.x < 0f
Less or equal was the key. With me worked perfectly.
@hardwir3d unfortunately for me this solution doesn't work
Then the problem must be outside of this block of code. Can you post some additional code u use?
@hardwir3d i already checked all the project and there is nothing that changes the y scale value. I also tried to change it on start function just to see if i could do it from there and i can't! How is that possible? Sorry anyway, i'm currently working for another one's project and i can only post very little parts of code.
Remove everything but your script. Is it working that way?