- Home /
Question by
Calvonator · Mar 05, 2021 at 12:16 PM ·
sprite2d game2d spritesaim
Right side of turret sprite is facing mouse rather than the front of the turret
Basically the title explains it.
I've got a turret sprite that I want the front (barrel side) of to face my mouse.
Unfortunately what is actually occurring is the right side is facing the mouse instead.
Here is a GIF of whats occuring:
https://gyazo.com/b239d0303cdc071abf90f2d952f97a98
void Update()
{
Vector3 mousePos = Input.mousePosition;
mousePos.z = 5.23f;
Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}
Comment
Best Answer
Answer by unity_ek98vnTRplGj8Q · Mar 05, 2021 at 04:37 PM
It looks like your default turret rotation is facing up, but a 0 degree mouse angle is to the right if you use Tan(y/x). Either rotate the sprite so that a rotation of 0 faces right or just subtract 90 degrees from your calculated angle.
Your answer
![](https://koobas.hobune.stream/wayback/20220613030234im_/https://answers.unity.com/themes/thub/images/avi.jpg)