- Home /
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.
Answer by WillTAtl · Nov 28, 2011 at 04:37 PM
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!
Thanks for the reply. I know a little math, but I'm not an experienced programmer. I asked for advice to avoid writing tons of code to do something that maybe you can do with a single command... I apologize for the repeated questions. I will try to follow your advice. Thank you again.
I'am really struggling to get my thing working. I'm using your script but something issn't working he always give the error: $$anonymous$$ identifier 'mousePos' and $$anonymous$$ identifier 'relativeToPoint'. I really don't know what the problem is. I want to make a 2d game and a barrel is pointed at my mouse. I'm a real noob.
This is the complete script http://answers.unity3d.com/questions/189003/drag-zoom-window.html I'm novice on Unity. $$anonymous$$aybe you don't remenber to declare variabile at top of script? Hope some expert user help you.
Your answer
Follow this Question
Related Questions
Checking if mouse dragged from one point to another 2 Answers
Move object towards mouse position 1 Answer
[2D] Angle to Vector conversion 1 Answer
How To Drag any Cube Using Mouse Click and Moved Only 1 Unit?? 2 Answers
Problems at using touch and buttons inputs,Problems at using touch input on Android 1 Answer