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 /
avatar image
0
Question by Malyglut · May 21, 2019 at 04:46 PM · mathanglevectoranglescalculations

Calculate launch angle based on launch direction

I'm working with a setup like this:

alt text

Where the vectors all start at transform.position and end as follows:

  • Black: transform.position + HandlePosition

  • Blue: transform.position + Vector2.right

  • White: transform.position - HandlePosition.normalized

The idea is that i can move the red handle around to change the White vector (the launch direction of the object).

Now what I would like to get is the angle between the White and Blue vectors.

I want to get this angle so that I can calculate the trajectory of the object and display a trajectory line.

Edit:
Given the example shown on the picture, I would expect an angle of about 45 degrees.

vectors.jpg (23.7 kB)
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

2 Replies

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

Answer by Malyglut · May 21, 2019 at 05:09 PM

The solution was to calculate a new vector based on the White vector and then use it in the Mathf.Atan2 method.

 Vector3 newVector = transform.position - White;
 float angle = Mathf.Atan2(newVector.y, newVector.x) *Mathf.Rad2Deg;  

This however gives values ranging from -180 to 180, to convert this to a 0 - 360 degree value I simply added 180 to the result of previous calculation.

 angle+= 180;

This works nicely for what I'm trying to do.

Thanks to @Pangamini for guiding me in the right direction.

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 Bunny83 · May 21, 2019 at 05:41 PM 1
Share

Note that adding 180° will not give you the angle between your white and blue vector when using the white direction vector but between your blue and black line. So you essentially use your "HandlePosition" which doesn't seem to be a position but a relative direction vector.


Since "White" is equal to transform.position - HandlePosition.normalized and your "newVector" is transform.position - White you just did calculate:

 newVector = transform.position - (transform.position - HandlePosition.normalized)

This is the same as

 newVector = HandlePosition.normalized

So you calculate the angle between the blue and black line and then add 180°. btw: Atan2 doesn't need a normalized vector.

avatar image Malyglut Bunny83 · May 21, 2019 at 06:13 PM 0
Share

Yes, you're completely right. I missed that. Is what I'm doing right now a very hacky approach? If so, what would be the correct way of tackling the problem of calculating the angle between Blue and White?

avatar image Bunny83 Malyglut · May 22, 2019 at 08:56 AM 1
Share

Well, the white direction vector is just

 Vector3 white = -HandlePosition;

The angle is just

 float angle = $$anonymous$$athf.Atan2(white .y, white .x) * $$anonymous$$athf.Rad2Deg;  

To make the range be 0 to 360 ins$$anonymous$$d of -180 to 180 just do this afterwards:

 if (angle < 0)
     angle += 360;
avatar image
2

Answer by Pangamini · May 21, 2019 at 04:51 PM

Check out Mathf.Atan2

Comment
Add comment · Show 5 · 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 Malyglut · May 21, 2019 at 04:53 PM 0
Share

Which vectors would I input into the $$anonymous$$athf.Atan2?

avatar image Pangamini Malyglut · May 21, 2019 at 04:57 PM 1
Share

"Return value is the angle between the x-axis and a 2D vector starting at zero and ter$$anonymous$$ating at (x,y)."

avatar image Pangamini Pangamini · May 21, 2019 at 04:58 PM 1
Share

The vector that describes the direction

avatar image Bunny83 Malyglut · May 21, 2019 at 05:25 PM 0
Share

btw: Atan2 is not specific to Unity. It exists in almost any math library. It just does arctan(y/x) and does a quadrant selection depending on the signs of x and y. It also handles problematic cases like the case when x == 0.

avatar image Pangamini Bunny83 · May 22, 2019 at 09:12 AM 0
Share

Yeah, I am kind of happy that Unity didn't invent their own off-world math :D

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

117 People are following this question.

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

Related Questions

Check if position is inside area? 2 Answers

How to find direction Vector for a collision surface? 1 Answer

2D angle of two Vec3 0 Answers

How can I calculate the Vector3 b using Vector 3 a pos with an angle? 1 Answer

[MATH] Vectors, magnitudes, and bears; Oh My! -Solved 2 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