- Home /
Top Down Sprite Facing Mouse
I have a top down game where my character sprite rotates towards the mouse and faces it when clicked. However, the character seems to have no idea which way is his face and which way is his butt. Sometimes he will face the mouse with his face, other times his butt. Here is the code i'm using for rotation:
Quaternion newRotation = Quaternion.LookRotation(InputTouch.hitpoint - player.transform.position);
newRotation.x = 0f;
newRotation.y = 0f;
player.transform.rotation = Quaternion.Slerp (player.transform.rotation, newRotation, Time.deltaTime * 300);
Thanks for the help!
Is there anyone who might be able to help me with my question?
Are you trying to do this over time or instantaneously? Also, make sure that the player.forward is actually point in the correct direction.
You are setting newRotation.x and newRotation.y as though newRotation was a Vector3/eulerangle (x, y, z) ins$$anonymous$$d of a Quaternion (w, x, y, z). To zero out the x and y parts you would have to do something like.
Quaternion newRotation = Quaternion.LookRotation(InputTouch.hitpoint - player.transform.position);
Vector3 newRotEul=newRotation.eulerAngles;
newRotEul.x = 0f;
newRotEul.y = 0f;
player.transform.rotation = Quaternion.Slerp (player.transform.rotation, Quaternion.Euler(newRotEul), Time.deltaTime * 300);
However, this will probably not work as eulerangles are not unique and using Quaternion.eulerangles does not always give the expected rotation. I'm not sure how you are getting InputTouch.hitpoint but if done right you shouldn't even have to zero out the rotation on the x and y axis so I would focus on getting the right rotation ins$$anonymous$$d of trying to fix an incorrect one.
Hmm, I wasn't able to get that rotation script to work at all maccabbe. I don't think I explained the issue properly and i'm not entirely sure how to describe it, so i'll show it ins$$anonymous$$d.
Sorry about the low quality. But as you can see in that, the rotation script is working properly except the character itself doesn't know the difference between his face and his butt.
Answer by fzawd · Jun 09, 2015 at 06:26 AM
Firstly, never manipulate a Quaternion
's parameters directly. Quaternion
s use advanced math to work and unless you know what you're doing, manipulating their parameters will usually break them. See the `Quaternion` manual page for more info.
For the problem at hand, I would suggest calculating a 'look-at' vector for your gameObject.transform
; and then use simple math to calculate a rotation. Assuming your calculated TouchInput.hitpoint
is in world-space coordinates, this code should work:
Vector3 lookAtVector = InputTouch.hitpoint - player.transform.position;
float angle = Mathf.Rad2Deg * Mathf.Atan2(lookAtVector.y, lookAtVector.x);
Quaternion newRotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 300);
Note that the z parameter of the look-at vector is not used, so we won't get strange behavior for the cases when player.transform.position.z
differs from TouchInput.hitpoint
.
How it Works:
First line calculates a look-at vector that (as the name suggests) gives us the direction our character needs to look-at.
Mathf.Atan2 will do the magic for us: It takes distances along y and x axis (in that order) and calculates a single float value: The desired angle of our character, relative to the world-space forward direction. We need to convert this number to degrees before it's usable, hence Mathf.Rad2Deg
.
Finally, we pass this info to Quaternion.AngleAxis(). Based on our float angle
, we will have a Quaternion facing angle
degrees from Vector3.forward
. This Quaternion can be used in any future operations.
I'll be honest, I'm not entirely sure how that code works. But, it does! Thank you very much for your help!
I edited my answer and added a little bit of explanation about the logic behind the code. As always, hope this helps :)
Wow, awesome! That helps a lot. Thanks for going above and beyond for me!
Answer by Mark Gossage · Jun 09, 2015 at 06:30 AM
Your question is very similar to http://answers.unity3d.com/questions/630670/rotate-2d-sprite-towards-moving-direction.html
Taking their answer & adapting for your code:
Vector3 moveDirection=InputTouch.hitpoint - player.transform.position;
float angle = Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);