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
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
SUBSCRIBE for More Unity Tutorials! Discord this unity tutor. P = atan2(Y,X) returns the four-quadrant inverse tangent (tan-1) of Y and X, which must be real.The atan2 function follows the convention that atan2(x,x) returns 0 when x is mathematically zero (either 0 or -0). And thank you for taking the time to help us improve the quality of Unity Documentation. Your name Your email Suggestion. Submit suggestion. Switch to Manual. Public static float Atan2 (float y, float x); Description. Returns the angle in radians whose Tan is y/x. I have a simple question on usage of math.Atan2 function with Unity in a 3D environment. I found this code on Unity's documentation for Math.Atan2. I changed it a bit to fit my needs. Vector3 waypoint = Random.insideUnitSphere + new Vector3(Random.Range(minX+1.5f, maxX-1.5f),prevPosY,Random.Range(minZ + 1.5f,maxZ - 1.5f)); //waypoints are.
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).
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:Glsl Atan2
All right but, how do we go from the formula above to the Unity’s one which basically is:
Unity Games
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
![Unity Unity](/uploads/1/1/8/9/118946972/165532211.gif)
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.