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 VoidChimera · Nov 27, 2014 at 07:05 PM · mathanglecalculation

Angle calculation not working??

I've been trying to calculate the angle between two Vec2's, but for some reason the way i've learned to do it via trig isn't working, and instead is giving me completely incorrect angles (in radians, and between 0 and 2π, which is kinda good at least because that's the expected range, but the angle itself is seemingly placebo-random within that range). This is the method i'm using (please excuse the awful writing): alt text

Anyone know why the heck this isn't working? I've tried this with Atan and Atan2, but neither works :/

This is the code i've been using (the object starts at origin, removing the need for subtraction beforehand):

  float angle = Mathf.Atan(target_pos.y/target_pos.x);


Anyone know what i'm messing up? It's giving me really crazy angles, like negatives when it's in the 2nd quadrant -_-

trig.png (29.6 kB)
Comment
Add comment · Show 4
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 richyrich · Nov 27, 2014 at 07:17 PM 0
Share

If you know the start point s, and also the two target points t1, t2, then you don't need trig to work out the angle, you can use Vector3.Angle...

avatar image richyrich · Nov 27, 2014 at 07:22 PM 0
Share

float angle will give you the value in Radians, not degrees. The conversion to degrees requires the additional line of code:

 angle = angle * $$anonymous$$athf.Rad2Deg;
avatar image richyrich · Nov 27, 2014 at 07:25 PM 0
Share

What only returns 90? Vector3.Angle can return values up to 180. However you can also add some clever code (that figures out which side of a line the new point is) to allow you to figure out the full angle.

avatar image VoidChimera · Nov 27, 2014 at 07:26 PM 0
Share

I'm using radians, used to working with them more so everything else in my code is set up for that

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by tanoshimi · Nov 27, 2014 at 07:09 PM

"isn't working" doesn't give us much to go on when we can't see your code, know what value you expected, or got. Please post a reproducible example. You are working with radians, right?

Comment
Add comment · Show 1 · 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 VoidChimera · Nov 27, 2014 at 07:12 PM 0
Share

Sorry, I guess [code] [/code] doesn't work... I'll make the code a bit more visible and rephrase the first part

avatar image
0

Answer by zharik86 · Nov 27, 2014 at 07:26 PM

For most case, better use Mathf.Atan2:

  float angle = Mathf.Atan2(target_pos.y, target_pos.x); //return angle in radians

But, in your line can be devision by 0 (target_pos.x = 0). Of course, you have exception. In Atan2() in this case angle is equal 90 degree. I hope that it will help you.

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Math Genius NEEDED! Algorithm problem with parabola 1 Answer

Find angle between two gameobjects? 1 Answer

How to calculate the angle of a trajectory to hit the target 1 Answer

Calculate a new position instead of using Vector3.back 1 Answer

How do I find slope on a spherical terrain 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