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
1
Question by Kody · Dec 31, 2012 at 01:38 AM · vector2geometry

Why would Vector2.Angle return wrong values

I run this code

 float angle = Vector2.Angle(d, d2);
 
 Debug.Log(d);
 Debug.Log(d2);
 Debug.Log(angle);

The debug log then reads:

 (1.0, 0.0)
 (0.0, 1.0)
 87.13759

When obviously the Debug.Log(angle) line should output 90.0. The same thing happens if I use Vector3.Angle by constructing Vector3s out of d and d2 and leaving their z values at 0.0f.

This is only happening if the Vector2s I use are calculated though. If I manually input

 d[0] = 1.0f;
 d[1] = .0f;
 d2[0] = .0f;
 d2[1] = 1.0f;

before running Vector2.Angle() it returns the expected 90.0f. But that is, of course, of no use. I'm trying to find if the directions of two line segments are or are very close to 90°. Does anyone know why Vector2.Angle() is returning such odd values, or know of a better way to determine if two direction vectors are 90° different from each other?

If it makes any difference the vectors d and d2 are computed as simply

 Vector2 d = (b - a).normalized;
 Vector2 d = (c - b).normalized;

With a, b, and c being vectors that are input in the inspector.

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
2
Best Answer

Answer by Lovrenc · Dec 31, 2012 at 01:53 AM

I guess Vector2.ToString() is rounding values for nice output and your angle is correct. Implicating that function returns 2 different values is kinda off (UNLESS you get different values EVERY time. that would mean V2.angle is undeterministic and has a bug. Unlikely though).

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 Owen-Reynolds · Dec 31, 2012 at 03:58 PM 0
Share

And can "turn off" rounding by printing both parts yourself: Debug.Log(d.x+"/"+d.y); Will probably see something like 0.96/0.04.

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

10 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

Related Questions

A shader that uses lighting but ignores it's own mesh/normals for lighting. 0 Answers

Why Physics perfomance cost get huge when static geometry is added ? 0 Answers

Projecting a rectangle into screen space 3 Answers

Domain shader create geometry 0 Answers

How do I calculate the arc of a throw, given two positions and an angle of attack? 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