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
1
Question by eskelinentopi · Apr 13, 2018 at 06:42 AM · vector3mathangles

Calculating relative angles from Vector3's

So, I have a little problem in my hands. I need to calculate an angle based on only Vector3 coordinates. I already tried using Vector3.Angle, but that's not giving me the result I want to achieve, as the angle is not the angle related to the other vertices of the shape, but rather relative to somewhere else. I'm not really too good at writing out what I'm trying to do, but if anyone has any idea about what I should do, I can try to give more precise information of my project.

Here's a little example:

alt text

So, any help appreciated :) It might be an easy thing to do, but I really have no clue about it.

angleexample.png (8.2 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

1 Reply

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

Answer by Harinezumi · Apr 13, 2018 at 08:34 AM

I think if you subtract the reference point from the other 2 points (basically moving them into its system of reference), then call Vector3.Angle() on those vectors, you will get the desired result.

Comment
Add comment · Show 7 · 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 · Apr 13, 2018 at 10:41 AM 1
Share

Right, if we call the left point "A", the right top point "B" and the lower right point "C" it's simply

 float angle = Vector3.Angle(B-A, C-A);

"B - A" gives you the vector from A to B

avatar image eskelinentopi Bunny83 · Apr 14, 2018 at 08:01 AM 0
Share

Thank you, this came in handy :D

avatar image eskelinentopi · Apr 14, 2018 at 08:01 AM 0
Share

Amazing! This works wonders. Now I'm just thinking: Is there a way to see if the angle exceeds 180°, and then do other stuff based on that? Because Vector3.Angle practically never gives a result above 179.9°. I would like it to be so, because in my project I have editable shapes like triangles, hexagons, etc. and I need to have it like so it never lets an angle to be over 180°, so it should never be a reflex angle. If that's not possible, it's not a big issue, just wondering whether I could somehow do it.

avatar image Bunny83 eskelinentopi · Apr 14, 2018 at 08:24 AM 1
Share

Just use Vector3.SignedAngle ins$$anonymous$$d of Vector3.Angle. Note that you have to provide an additional axis vector for reference. In your case either Vector3.forward or -Vector3.forward:

 float angle = Vector3.SignedAngle(B-A, C-A, Vector3.forward);


avatar image eskelinentopi Bunny83 · Apr 14, 2018 at 05:52 PM 0
Share

It says that "UnityEngine.Vector3 does not contain definition for SignedAngle". What am I doing wrong? I did the same as in your example.

Show more comments

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

100 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

Related Questions

Average of Vectors [Math] 1 Answer

Rotate 3d vector based on local forward 1 Answer

Make something move away from click point. 2 Answers

Calculating endpoints of directional vector 2 Answers

Align line of units based on vectors 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