Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by benfattino · Nov 28, 2011 at 03:58 PM · positionmouseanglevector2convert

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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!

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image benfattino · Nov 28, 2011 at 04:48 PM 0
Share

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.

avatar image kobey · Oct 30, 2012 at 05:00 PM 0
Share

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.

avatar image benfattino · Oct 30, 2012 at 05:52 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges