# Changing sprites based on rotation?

Hey guys, I’ve got a new question about sprites, I’ve been looking at rotating and animating them some more and I was wondering if there were any tutorials about changing sprites based on the mouse position. So when you move the mouse clockwise or anti-clockwise around the player the player’s sprite changes to match the direction the mouse is facing.

It’s easy enough to do with keys, I found a tutorial by gamesplusjames, but I’d like to know about how to do it with just the mouse.

The closest I can think of is hotline miami, they seem to have done it completely top down though where you can only see the head and a tiny portion of the body but I was thinking of something a bit more angled like the original Command and Conquer where you can see more of the body when the sprites are switching angles.

Starcraft and warcraft is a good example, do this:
get the Difference between the mousePos and the middle of the screen
normalize this using Vector2.normalize
after normalizing, get the angle by angle = tan^-1(diffX, diffY)
then state = angle / 45 in case of an 8 way sprite, and then just a simple switch case should do the trick

(tan^-1 = Mathf.Atan2)

Oh gawd, that doesn’t look simple to me, I’m probably going to have to find a tutorial on it, I’m currently looking at a top down shooter tutorial by quill18 who explains that kind of maths for rotation a bit but I’ll have to do some digging so I can see it all play out.

And yes! Starcraft and Warcraft are classic examples, I like the style of that, I was thinking of doing something similar, I always liked the sprite style of the original Starcraft in particular.

It’s actually pretty basic:
you can get the difference between the mouse cursor position and the center of the screen by Input.mousePosition - new Vector2(Screen.width, Screen.height).
You don’t need to normalize it, forget that
tan^-1 basically returns the angle between horizontal, and the line between Vector2.zero and your coords
I want to change the last part:
What you want to do is Mathf.Round(angle / 45). What this does, is if you have an angle, it will basically get the state the character should be in, state 0 is between -22.5 and 22.5, state 1 is between -22.5 and 67.5 and so on until state 7. This way you can create a switch case statement based on that value and change the sprites that way

Okay… I think I’m starting to get it, the basic idea seems to be that you change the sprite based on the GameObject’s rotation right?

If that’s the case, why use a switch case? Couldn’t you just use an if statement to check the rotation of the GameObject on a certain axis and then have the image sprite change based on what number it reaches? Sort of like when you have a standard integer check and the number goes below or above a certain amount.

Not really, it’s based on the position of the mouseCursor, and a switch…case is a value checker, so
int a = 4;
switch (a) {
case 3:
Not going to happen, sorry
break;
case 4:
this will happen
break;
}
in my opinion it looks clearer than an if-else chain, and it has a “default” option for safety

Thanks for the explanations, I’ll just have to experiment then and see how it all works.