• 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
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

People who like this

0 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 (Mathf.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 aiming 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
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
Peter G
Fattie
Necronomicron

People who like this

3 Show 0 · 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

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

People who like this

0 Show 0 · 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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our 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

finding position of second vector when first vector and angle between two vectors is given 3 Answers

How to get rotation for projectile to fire at cursor? 1 Answer

How to stop a Gameobject moving on the X axis 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