Quantcast
Channel: Answers by "hoekkii"
Viewing all articles
Browse latest Browse all 26

Answer by hoekkii

$
0
0
Short answer: public static void Normal2PitchYaw(Vector3 normal, out float pitch, out float yaw) { yaw = -Mathf.Asin(normal.x) * Mathf.Rad2Deg; pitch = Mathf.Atan2(normal.z, normal.y) * Mathf.Rad2Deg; }
Long answer:
What do you really want to do?
Because working with pitch, yaw, roll can be a real pain in the.
So I would recommend to use quaternions instead.
If you really need pitch, yaw roll..

Got all the info from this page: https://stackoverflow.com/questions/1568568/how-to-convert-euler-angles-to-directional-vector

You will see on the page; to go from pitch, yaw to a direction vector you'll need this formula: x = cos(yaw)*cos(pitch) y = sin(yaw)*cos(pitch) z = sin(pitch)
To convert this to Unity you can't copy paste it, because in Unity the Y-axis is up. so you will get this: x = -sin(yaw) * cos(pitch) y = cos(yaw) * cos(pitch) z = sin(pitch)
You now can invert the equation to get from direction to pitch and yaw. So you would expect: pitch = asin(normal.z) yaw = -atan2(normal.x, normal.y)
This is a valid answer, but when applying it raw to a transform like `transform.eulerAngles = new Vector3(pitch, 0, yaw)` wouldn't get the expected result because the rotation will (as a rough explanation) be `Quaternion.Euler(pitch, 0, 0) * Quaternion.Euler(0, 0, yaw)` but the multiplication should be the other way around. which leads us to: yaw = -asin(normal.x) pitch = atan2(normal.z, normal.y)

Edit:

Thank you for clarifying your question.
What you want to do is way more straight forward than the story above.
The target is to get the forward direction vector of the slope, which can be converted in to a rotation.
You can use the cross product to get the right (as in direction) vector by doing the cross product of the normal and the up direction.
With the normal and the right vector you can do a cross product again to get the forward vector.

Which in code looks like: static Vector3 Normal2Forward(Vector3 normal, Vector3 up) { var right = Vector3.Cross(normal, up); return Vector3.Cross(normal, right); } void Foo() { var forward = Normal2Forward(hit.normal, Vector3.up); var rotation = Quaternion.LookRotation(forward, Vector3.up); }

Viewing all articles
Browse latest Browse all 26

Latest Images

Trending Articles





Latest Images