Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed May 19, 2013 at 07:27 AM by fafase for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by thatbigguy · May 20, 2011 at 05:48 PM · 2danglevectory-axisx-axis

Calculating the Angle between two vectors

I am making a 2D game where I have to aim a grapple onto a wall. I need help calculating the angle, because I want the game to look like this:

90 degrees

^

|

|--------->0 degrees

the vertical component is the y-axis, and the horizontal component is the x-axis

Comment
Add comment · Show 2
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 Owen-Reynolds · May 20, 2011 at 06:14 PM 0
Share

Vector3.angle? Vector3.dot, or Quaternion.dot, is probably faster under the right circumstances, if you enjoy trigonometry. If the ground is always flat, could use arctangent ($$anonymous$$athf.Atan2, but only if you need the speed-up and like trig.)

avatar image thatbigguy · May 21, 2011 at 03:40 PM 0
Share

Pretend a bowman is shooting an arrow. I want to simulate him ai$$anonymous$$g the arrow between 0(straight across the x-axis) and 90 degrees (straight up the y-axis) with the help of the mouse drag

2 Replies

  • Sort: 
avatar image
3
Best Answer

Answer by almo · May 20, 2011 at 06:07 PM

 Mathf.Acos(Vector3.Dot(vector1.normalized, vector2.normalized));

should give you the angle between two vectors. This answers the title of your question, but the body of your question is very vague.

Comment
Add comment · 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
0

Answer by Owen-Reynolds · May 21, 2011 at 03:56 PM

For angle to the mouse in a 2D, need to know the mouse is in screen Pixels and the bow is in world units. Can convert the bow to screen pixels, or can convert the mouse to the world position (in 3D the world position of the mouse is anywhere on the line it shoots, for 2D it's just a point.) These are some fo the commands (untested):

 Vector3 bowP = Camera.main.WorldToScreenPoint(bowWoldPos);
 // x,y are in pixels. z can be ignored

 Vector3 aimP = Input.MousePosition();
 // Same units: x,y are in pixels
 // acos (see other answer) for the angle

 OR, to use world pos:

 Vector3 aimWorldP = Camera.main.ScreenToWorldPoint(Input.MousePosition());
 aimWorldP.z = bowPosition.z;
 bow.LookAt(aimWorldP);
 // bow.forward now aims at the mouse
Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Issues calculating angle in 2D 0 Answers

Rotating object to face mouse with physics isn't working 2 Answers

I have a serious problem! 1 Answer

Does unity implement a way to convert an angle+magnitude representation of a vector to an x,y vector? 1 Answer

360 Controller and angle problem 0 Answers


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