In this article I’ll briefly explain how you can rotate an object, using Unity, in order for it to “look” at the mouse position.
TL;DR
Returns the arctangent of two values (x,y). The arctangent of (y,x). The signs of the x and y parameters are used to determine the quadrant of the return values within the range of -π to π. The atan2 HLSL intrinsic function is well-defined for every point other than the origin, even if y equals 0 and x does not equal 0. Type Description.
- Atan2 calculates the arctangent of y/x. Atan2 is well defined for every point other than the origin, even if x equals 0 and y does not equal 0. For vectors, the returned vector contains the arctangent of each element of the input vector. Reference Implementation. Atan2 for a float2 scalar could be implemented as an approximation like this.
- The C# math library used in Unity providing vector types and math functions with a shader like syntax - Unity-Technologies/Unity.Mathematics.
- Unity is the ultimate game development platform. Use Unity to build high-quality 3D and 2D games, deploy them across mobile, desktop, VR/AR, consoles or the Web, and connect with loyal and enthusiastic players and customers.
Here’s the code snippet which will make the player transform point towards the mouse position. Please read below if you’re interested in how this works.
Explanation
Let’s start with the definition Math.Atan2 that you can find in the Unity documentation:
Returns the angle in radians whose Tan is y/x.
Return value is the angle between the x-axis and a 2D vector starting at zero and terminating at (x,y).

And that’s why, at line 7, we provide the x and y of the mouse position (multiplying everything by the radians-to-degrees conversion constant). But where does all this come from?

To understand that, we need to dust off some old mathematics memories and make use of some linear equation formulas. And so, given two lines:
and their respective slopes m1 and m2, the angle between them is given by:
All right but, how do we go from the formula above to the Unity’s one which basically is:
Simply by considering the first line as the x-axis, and the second one (the one pointing to the mouse) as passing from zero. That gives us the following:
And by replacing slopes with their respective values, we end up with:
Applying the simplified formula in Unity
In order to use this simplified version in Unity, we need to apply some tweaks. The easiest way to accomplish this is to move from the world-space (measured in units) to the screen-space (measured in pixels). The screen-space comes very handily in this case because it emulates the “first quadrant” of a “Cartesian plane”, where all the x and y are positive. Find more info here.
In order to work on the screen-space, we fetch the mouse position using Input.mousePosition (line 3), and then we have to transform the player position from world to screen coordinates with mainCamera.WorldToScreenPoint(player.position) (line 4). After that, we just need to translate the points (lines 5/6) in order to have the player in (0,0) (remember that Unity’s formula implies the first line to be the x-axis).
Once done, we should end up in a situation like this, and therefore being able to apply our simple formula to retrieve the correct angle and then apply a rotation to the player’s transform (line 8):

Wrapping up
Unity is a very well done game engine and it perfectly suits both experts and beginners users by hiding some of the complexity involved in the game development world. But I think that sometimes it is worth to understand what’s really going on behind those very simple methods. Whether it is just for curiosity or professional needs, it should help us improve in what we do.
That’s why I hope this article results useful for anyone out there. If you have any question, please, feel free to leave a comment.
-->Returns the arctangent of two values (x,y).
| ret atan2(y, x) |
|---|
Parameters
| Item | Description |
|---|---|
| y | [in] The y value. |
| x | [in] The x value. |
Return Value
The arctangent of (y,x).
Unity Atan2
Remarks
The signs of the x and y parameters are used to determine the quadrant of the return values within the range of -π to π. The atan2 HLSL intrinsic function is well-defined for every point other than the origin, even if y equals 0 and x does not equal 0.
Type Description
| Name | Template Type | Component Type | Size |
|---|---|---|---|
| y | same as input x | float | same dimension(s) as input x |
| x | scalar, vector, or matrix | float | any |
| ret | same as input x | float | same dimension(s) as input x |
Minimum Shader Model
This function is supported in the following shader models.
| Shader Model | Supported |
|---|---|
| Shader Model 2 (DirectX HLSL) and higher shader models | yes |
| Shader Model 1 (DirectX HLSL) | vs_1_1 |
What Is Atan2

Requirements
| Requirement | Value |
|---|---|
| Header |
|
See also
