- Home /
If I call flipx on the spriterenderer of an image, why doesn't the sprite attached to the image flip?
I have a strange issue related to the flipX varialble of a sprite renderer.
Consider the following code:
leftImageSpriteRenderer = leftImage.GetComponent<SpriteRenderer>();
if (leftImageSpriteRenderer != null)
{
Debug.Log("Found the sprite renderer");
leftImageSpriteRenderer.flipX = true;
}
leftImage.sprite = AssetDatabase.LoadAssetAtPath<Sprite>("Assets/Portraits/character_portrait.png");
My expectation would be that the character portrait would be flipped along the X axis. However, while the sprite appears, it is not flipped. IS the problem that I'm flipping the spriterender component and referring the sprite in the parent image object?
Thanks for your attention!
Answer by KittenSnipes · Jan 29, 2018 at 02:52 AM
Its because to affect things like colliders and the actual sprite you have to use the negative of your current scale.
SpriteRenderer.flipX:
public bool flipX;
Description Flips the sprite on the X axis.
Only the rendering is affected. Use negative Transform.scale, if you want to affect all the other components (for example colliders).
Thank you for answering. I am looking into this to fully understand your reply.
Your answer
Follow this Question
Related Questions
Gravity Change + Flip 1 Answer
Flipping a group of sprites 1 Answer
Unity 2D, Flip Sprite without using a rigidbody 1 Answer
Is there a way to create random polygonal sprite in script? 0 Answers