# Convert vector2 mouse position into angle.

How i convert vector2 mouse position into angle (relative to screen point, example 0,0 or another). I need for rotate GUI in real time. Thanks.

pretty easily, actually! Not strictly a unity question, and google is your friend - typing your exact question into google, this was my first hit, about half-way down the page the example “Magnitude and Direction from components” shows the math - the angle of a 2d vector is tan^-1(y/x). From there to code is fairly easy - if you know what you’re doing. If you don’t, and know trig mainly from typical non-programming math classes, there are a few odd things that aren’t obvious at first.

Tan^-1 is the inverse tangent function, also known as arctangent, and in code is usually implemented as a function called atan. You’ll specifically want to the variant Atan2, which takes the x and y components separately instead of pre-divided. The first time I tried coding something like this, back in high school, I had to write a lot of unnecessary code to fix the values output by the regular atan function manually before stumbling on atan2!

Code example…

``````//get the vector representing the mouse's position relative to the point...
var v:Vector2 = mousePos - relativeToPoint;

//use atan2 to get the angle; Atan2 returns radians