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 mars_unity805 · Jul 16, 2020 at 02:46 PM · vector3bug-perhapsangles

Vector3.SignedAngle seemingly avoids values close to 0 and +-180

Hello!

I'm using Vector3.SignedAngle to calculate an approximate yaw of an object, relative to world z axis:

 var handleYaw = Vector3.SignedAngle(Vector3.forward, grabHandle.transform.forward, grabHandle.transform.up);

I've been logging the value of handleYaw while rotating the grabHandle object.

While grabHandle's up vector mostly (but not perfectly) aligns with World up (Vector3.up), the values get weird when its forward vector starts aligning with axis created by Vector3.forward.

Instead of the angle getting close to 0 or +-180, it simply flips the sign. So let's say I have an angle of 10, and I move 1 degree counter-clockwise. Instead of the new angle being 9, it's something like -9.5. Or if it's 173, and I move 1 degree clockwise it's suddenly -174.

This only happens as the angle gets close to 0 or +-180, it doesn't happen otherwise. Does anyone know what's going on here?

Is it some sort of confusion with gimbal lock as two of the grabHandle's axes align with World Up and World Forward? Or maybe something entirely else?

Any help towards fixing the issue would be greatly appreciated.

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

Answer by Bunny83 · Jul 16, 2020 at 05:11 PM

Well, your issue is that when your two vectors are relatively close to each other the 2d plane that the two vectors describe can easily flip around to the other side. SignedAngle still calculates the actual angle between the two vectors, not the angle in a certain plane. The given axis vector is only used to determine the sign of the angle. So imagine your forward vector is pointing exactly forward but is tilted upwards by 9°. Then SignedAngle will still give you an angle of 9°. However the normal of the plane your two vectors lie in points to the right. Now if you rotate slightly to the right that normal would move downward a little bit. Now that normal points in the opposite direction that your reference axis and the angle flips sides.


What you probably want is something like that:

 var fwd = grabHandle.transform.forward;
 fwd.y = 0;
 var handleYaw = Vector3.SignedAngle(Vector3.forward, fwd, Vector3.up);

This gives you the signed angle in the horizontal worldspace "x-z" plane.


Though in this case it might be simpler to just use Mathf.Atan2 with the x and z components of your forward vector.

 var fwd = grabHandle.transform.forward;
 var yaw = Mathf.Atan2(fwd.x, fwd.z) * Mathf.Rad2Deg;

Note that depending on where you want 0° and in which way the angle should increase you might have to add an offset to the result and wrap it accordingly.


ps: If you also looking for the pitch, this can simply be taken from the y component

 var pitch = Mathf.Asin(grabHandle.transform.forward.y) * Mathf.Rad2Deg;

while this would probably work fine in most cases, it's recommended to wrap the y value in a Mathf.Clamp(y, -1f, 1f) because Asin must not be fed with a value any larger than 1 or smaller than -1. If you do you get back a NaN (nor a number). Unity does the same thing in the Angle method

[1]: https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Export/Math/Vector3.cs#L319

Comment
Add comment · Show 2 · 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 mars_unity805 · Jul 16, 2020 at 05:48 PM 0
Share

Everything you write makes a whole lot of sense to me, I'm pretty sure you're spot on here! I'll test it out tomorrow and get back to you. Thanks for the response!

avatar image mars_unity805 · Jul 17, 2020 at 10:38 AM 0
Share

Alright, tested it out just now. Works like a charm, thanks for the help! :)

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

162 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 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

Setting Vector3.angle between two gameObjects 2 Answers

Compare two Vector3 Angles with percentage difference 3 Answers

Convert an angle to a vector? 1 Answer

Align line of units based on vectors 0 Answers

Set rotation based on 1,1,1 style vector? How to convert vector3 to quaternion? 1 Answer


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