Sprite facing direction of movement

I would like to make a sprite, rotate in the direction of it’s movement, and well, have no idea where to start.

First you will need the direction the sprite is moving in. That depends on your code. Let’s assume it is moving into its “right” direction:

Vector2 moveDirection = transform.right;

Next, since it is a sprite, you will want to rotate it so that its “up” vector (or any vector you want) is looking into the move direction, and that the “forward” vector stays the same. The “forward” vector is Unity’s Z direction, which doesn’t ever change for sprites.

Quaternion rotation = Quaternion.LookRotation(Vector3.forward, moveDirection);

Then, just set your Transform’s rotation:

transform.rotation = rotation;

If it is a character you are moving, you may notice that this will turn it on its side, turning their head into the move direction (“right” in the example.) That may not what you want. Instead, you may need to rotate it further to upright it again. You can do that by multiplying the necessary rotation Quaternion on top of the current rotation, for example:

transform.rotation *= Quaternion.Euler(0f, 0f, 90f);

(I can’t remember if it’s 90 or -90, try both :wink: )

1 Like

if you are new to unity 2D, this project will give basic understanding of things…

for ur problem, i think making negating(multiplying by -1) x-axis scale value will help.

1 Like