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
2
Question by tomekkie2 · Jan 17, 2012 at 12:29 PM · vector3.angle

Vector3.Angle noob question

who can explain why:

 Debug.Log (Vector3.Angle(Vector3.up,new Vector3(-1,-1,0)));
     
 Debug.Log (Vector3.Angle(Vector3.up,new Vector3(1,-1,0)));

both return same value 135, instead of 135 and 235. (The same with Vector2) and how to get around that?

Comment
Add comment · Show 1
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 Fattie · Jan 17, 2012 at 01:35 PM 1
Share

PS the very short answer is "it calculates the angle BETWEEN two vectors" -- the shortest path, not the angle "from one to another" in a certain direction. Long answer below.

2 Replies

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

Answer by Fattie · Jan 17, 2012 at 12:57 PM

Say you are standing up and looking ahead.

(-1,-1,0) is LEFT and DOWN ... at a 45 degree angle. Ok?

imagine a "tunnel" going in to the ground, to your LEFT, at a 45 degree angle.

(1,-1,0) is RIGHT and DOWN ... also at a 45 degree angle. Ok?

imagine a "tunnel" going in to the ground, to your RIGHT, at a 45 degree angle.

In fact! from BOTH those places, (from both the tunnels) to get UPRIGHT ... up to the sky ... is 135 degrees!

it makes no difference what "compass direction" the tunnel is pointing.

Can you see this OK now ?

Next: you ask about "235". I'm assuming you want it to go "all the way around" is that correct? "Vector3.Angle" won't help you with that because of course it goes the 'shortest way".

What you are talking about is actually a very difficult problem in game programming! But I suggest the simplest solution for you is, very simply in your example 360 minus 135 is 235. Just check if you are "on the right" and then you know you have to say 360 minus the number. You see what I mean?

if that doesn't work for you (or anyone reading in the future) your next step is to learn about Quaternions, and beyond that a lot of trigonometry and beyond that just more logic like "if I am down and to the right, I need to do THIS calculation ..." Its pretty annoying at any level!

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 tomekkie2 · Jan 17, 2012 at 01:08 PM 0
Share

References say: "Returns the angle in degrees between from and to." It suggested to me that it matters which one is from and which one is to and returns from - to. If it said just between, I would understand the shortest way which is the answer to my question.

avatar image Fattie · Jan 17, 2012 at 01:34 PM 1
Share

You are absolutely correct, $$anonymous$$e - the documentation could be clearer. I agree with you completely. Hope it helps!

avatar image tomekkie2 · Jan 17, 2012 at 01:38 PM 0
Share

Great thanks.

avatar image
1

Answer by aqez · Mar 04, 2012 at 02:03 AM

I found this while searching for the solution to this problem..

http://www.gamedev.net/topic/542870-determine-which-side-of-a-line-a-point-is/

This solved my issue, the final code looking something like..

         Vector3 difference = heading - p.ShipInfo.Position;
         Vector3 perp = new Vector3(-difference.y, 0, difference.z);
         float d = Vector3.Dot(hit.point - p.ShipInfo.Position, perp);
         Debug.Log(Mathf.Sign(d));
         float degrees = Mathf.Acos(Vector3.Dot(heading.normalized, (hit.point - p.ShipInfo.Position).normalized)) * Mathf.Rad2Deg;
         if (Mathf.Sign(d) > 0)
             degrees += 180;

         Debug.Log(degrees);

Side note, I work in x,z coordinates instead of x,y so replace where necessary.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Get angle beetween surfaces 1 Answer

Moving On Two Axes and Reading a Stable Angle 0 Answers

Forward angles 2 Answers

Achieving 80 degreed line of sight using Vector3.Angle, and detecting if there is something in the way ? 1 Answer

Hop from transform to transform in 2D game? 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