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
var angleRadians=Mathf.Atan2(v.y, v.x);

//convert to degrees
var angleDegrees = angleRadians * Mathf.RadToDeg;

//angleDegrees will be in the range (-180,180].
//I like normalizing to [0,360) myself, but this is optional..
if (angleDegrees<0) 
    angleDegrees+=360;

angleDegrees will be 0 if mouse is directly to the right of the object and increases counter-clockwise, so 90 degrees is up, 180 left, and 270 down, etc. If you skip the last bit and leave it -180 to 180, down will be -90 instead of 270.

This seems to be a reworked duplicate of your previous question, just simplified to a specific technical question rather more general question with a wall of code, which is a big improvement and makes it much easier for people to answer. If this is so, and this answer works for you, you ought to close that one so nobody wastes time answering a question you don’t need an answer to anymore! In the future, you could simply edit the original question to replace it if you come up with a better way to ask it. It will reduce your chances of accumulating negative karma from down-votes for repeated questions!