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 /
  • Help Room /
avatar image
0
Question by SlaxXx · May 04, 2020 at 04:15 PM · angletrigonometry

Correct use of SignedAngle?

I'm trying to get the angle between two vectors from the perspective of a plane. Sounds like exactly what SignedAngle(Vector3 from, Vector3 to, Vector3 axis) is supposed to do. However i'm not getting the result that i want and now i want to know if i don't use the function correctly, if the function is not doing what i think it is, or if it is just plainly a bug.

In short: if i move one vector parallel to the "axis" the returned angle also changes, even though viewed from "axis" the moved vector only differs in length to that perspective, not in angle..

Long story: I have 3 Nodes, A parent, the current Node, and a child. I'm trying to get the roll angle between the parent orientation (so the parents up vector) and the direction of the child.

alt text

So what i do is "look at the scene" from the parent's forward vector and determine the angle between the parents up vector and the direction vector of the child.

In code:

 Vector3 parentForward = parent.transform.TransformDirection(Vector3.forward);
 Vector3 parentUp = parent.transform.TransformDirection(Vector3.up);
 Vector3 childDirection = (child.transform.position - current.transform.position).normalized;
 
 float roll = Vector3.SignedAngle(parentUp, childDirection, parentForward);

If i now rotate the child around the parent on the global x axis (parents z axis) everything works as intended, as seen in the left image of the following illustration (viewed from the front = global -x axis).

If i however rotate the child in the global z axis (parents -x axis) the angle also gradually decreases, as if i had just rotated it for the same amount on the global x axis (right image, viewed from behind = global z axis)

But as the given rotation Axis is the global x axis, rotating around the z axis should not have an effect on the result, at least as far as i understand it. (at least not until i overshoot the 90° where the angle between parent and child turns from 180° to 0° instantly)

alt text

is this intended behaviour? if so, what is the parameter "axis" for?

fail.png (25.8 kB)
setup.png (16.4 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
0
Best Answer

Answer by SlaxXx · May 05, 2020 at 02:50 PM

Looking at the implementation of SignedAngle (https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Export/Math/Vector3.cs) this indeed seems to be intended behaviour. The "Axis" is only for determining the sign. I will attempt to implement my wanted behaviour of calculating the angle in the perspective of a given rotational axis and hopefully get back to you with my results.


Edit: The solution to this was actually really simple:

 public static float GetAngleOnAxis(this Vector3 self, Vector3 other, Vector3 axis)
 {
   Vector3 perpendicularSelf = Vector3.Cross(axis, self);
   Vector3 perpendicularOther = Vector3.Cross(axis, other);
   return Vector3.SignedAngle(perpendicularSelf, perpendicularOther, axis);
 }

What i did is calculate the Cross product of the axis and both vectors. What i essentially get from that is a projection of the vector onto the "viewing-plane" of the axis. it is indeed rotated by 90° after that, but as i'm rotating both vectors by the same amount, the relative angle stays the same.

Now that both angles are projected onto the plane i actually want the angle from, i can simply use SignedAngle again to get the correct angle this time.


DISCLAIMER: I'm currently on a tight schedule so i only confirmed that it looks right in my project. It is not properly tested and might return weird values under certain circumstances. Use with caution if values have to be absolutely positively right.

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

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

How to set face direction? 0 Answers

Rotating in relation to touch X on screen 1 Answer

angle problem 0 Answers

Cull length of ProjectOnPlane vector3 to bound of normal in direction of travel? 0 Answers

Rotate towards target in 2D space using AddTorque 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