Figuring out the direction of a face.

Hey, I need to raycast, and figure out the direction the hit face is facing (In angular form) I figure I need to base that off the normal, but I don’t now how to transfer a normal into a euler angle, or a quaternion. Any ideas?

If you want to get the angle between the normal and some reference direction, then Vector3.Angle will give you that. If you want the quaternion rotation from one to the other, Quaternion.SetFromRotation will give you that. And then if you want the euler angles, you can get that from the quaternion.

Depending on what you’re doing, dot product (Vector3.Dot) can be useful, too, as a relatively quick way to determine the relationship between two vectors, e.g. in backface culling, and I use it during collisions to check if it’s a direct hit or more of a glancing blow.

That sorta works, but lets say I raycast, and I use Vector3.Angle(hit.point, ?) what should I use for the second point to determine the x axis of that face?

An angle is a measure calculated from the orientation of two separate entities. You have explained that one of these entities is (the triangle), but not what the other is. Therefore it’s impossible to tell you what the second vector should be (although the first should definitely be hit.normal not hit.point).

Perhaps you want to know the angle with the ray used for casting, or perhaps the angle with the up vector.

If you explain what you are trying to do exactly, we maybe able to find out what the other vector should be.

I’m trying the same thing: find the rotation of the angle between two vector3 points I need the eulerAngles from that rotation.

SetFromRotation does not exist, there is a SetFromToRotation but I can’t get that one to work

(An instance of type 'UnityEngine.Quaternion' is required to access non static member 'SetFromToRotation'.)

And a FromToRotation.
I am not sure what the difference is between those, the later one works but the values I get change when I’m on level terrain.

I have a gameObject with a capsule collider + collider in it. I want to raycast down, and have it be facing away from the face that it collided with. Because the script lives in the gameObject, I have access to hit.point, and transform.position.

Try something like this:

theGameObject.transform.up = hit.normal;

If this does not orient the object over the right axis (“facing away” can mean many different orientations :P), you could try .right or .forward instead of .up.

o.o wow. Thanks man! I can’t beleave how simple it is. Rofl, that is epic, and works perfectly. Just wow. Hahaha.

Ok, so I got that to pretty much work pretty well, unless the face is facing downwards. Then it will randomly change the y rotation from 0-180 (in normal -1 to 0), anyone know how to fix that?