I am wondering if there are any problems with flipping a sprite on the x-axis using vector2 to tell which direction it is facing/flip. Though doing this helps me write less code for Ray-cast direction and creating single states with a single animation (using 1 animation player facing right than using 2 animation facing left and right). If there is a better solution please tell me and thanks. Btw I am still pretty new and learning.
1 Answer
A negative scale can adversely affect physics features. When you flip a shape backwards, collision/raycast checks that depend on the winding of the collider effectively see it as inside-out. So you might find the object becomes invisible to collisions/clicks when inverted. Changing the scale might also force the physics engine to recompute the object's inertia properties, or it may behave badly in other ways.
To avoid this, I usually like to keep the visual representation of my objects in a child object, while the physics stays at the parent (and colliders live either there or in a sibling). That way I can squash and stretch and flip and wiggle my visuals any way I like, without impacting the physics of the object.
Another method you can use is the flipX
property (and flipY
) of the SpriteRenderer
class. These can be used to reverse the sprite visually, without impacting other components attached to the object. You can also access these as checkboxes in the Inspector: