- Home /
Rotating a Sprite to face mouse / target?
EDIT: For anyone visiting this question: this was typed before Unity had any 2D tools so this might not be suitable for your 2D projects, or it might be an overly complicated way of dealing with the problem. Munchy2007's answer should work fine in most cases.
I've hit a wall here as I switched my very early 2d topdown shooter prototype from a 3D project to a 2D one, and started working with 2D sprites instead of planes with images.
I thought this would be a simple switch, but I'm having some problems getting the sprites to rotate properly. After searching and trying multiple solutions, I still get no results.
My game has Raycast Mouse look:
// Raycast Mouse Looking
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast (ray, out hit, 100)) {
Vector3 hitPoint = hit.point;
Vector3 targetDir = hitPoint - transform.position;
targetDir = new Vector3(targetDir.y, targetDir.x, 0); //I tried multiple different setups here, like (0, 0, targetDir.z) etc...
transform.LookAt(targetDir);
As you might see here, I'm trying to use LookAt() function to make the player Sprite rotate towards the Mouse position. This worked fine with 3D planes, but things got awry when I switched to the 2D settings and 2D View.
Two problems: Instead of rotating around one axis, the Sprite rotates the whole GAMEOBJECT, making the sprite warp and turn around like it was on a plane (or a 3D object).
On the other hand, if I get the sprite to stay facing towards the screen and turn around, the sprite turns around in a weird manner, and resets the rotation at certain points when moving the mouse around the sprite. It's like the sprite "Flips" without me ordering it to do so.
I want the sprite to rotate around the Z-AXIS, as in the default Unity 2D view Y is vertical and X is horizontal.
Any solutions? I'm guessing I'm making a very simple mistake somewhere...
Thanks!
Update: I tried an example I found, which does not involve Raycasting:
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
lookPos = lookPos - transform.position;
float angle = $$anonymous$$athf.Atan2(lookPos.y, lookPos.x) * $$anonymous$$athf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
This works like I want it to (one problem is that the sprite is facing the mouse position sideways ins$$anonymous$$d of forward, but I'll just turn the character around in Photoshop).
Thing is my game is an "arena" type of shooter, where the camera clamps to certain areas, which requires the mouse look to be a Raycast one. Otherwise the when near the edges the character will turn facing the wrong way.
Finally a solution to my sprite rotating problem!
I've just rotated the sprite 90° afterwards so that it faces the right direction.
Big thanks!
Answer by MrRightclick · Dec 18, 2013 at 02:35 PM
Answering my own question. I combined the two solutions up there, and ended up with this:
// Raycast Mouse Looking
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast (ray, out hit, 100)) {
Vector3 hitPoint = hit.point;
Vector3 targetDir = hitPoint - transform.position;
float angle = Mathf.Atan2(targetDir.y, targetDir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
And this seems to be working. The sprite is still rotated 90 degrees clockwise, so facing sideways (the image itself has the character facing down). Any way to fix this other than editing the picture? I like drawing my characters facing up & down.. :)
I am so glad I found this you just fixed my game. Thanks!
You math is a bit incorrect, in order for the sprite to align. Line 9 should be:
float angle = $$anonymous$$athf.Atan2(targetDir.x, targetDir.y) * -$$anonymous$$athf.Rad2Deg;
Answer by Munchy2007 · Feb 04, 2014 at 05:05 PM
I use this to make my sprite follow the mouse. The sprite default orientation is pointing upwards.
void Update () {
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position);
}
For some reason when i was using this code in my game it didn't seem to have a consistent rotation speed. Some angles would cause the "front" of my sprite to be slightly off center to the mouse.
With that said, using the Raycast method above, the exact front of my ship i always pointed directly at the mouse.
It could be because it is Vector3, I don't really know how to code.
Your answer
Follow this Question
Related Questions
Sprite rotation with animation (2d project) 0 Answers
Joystick 2D Sprite Rotation and Movement 0 Answers
Top Down Sprite Facing Mouse 2 Answers