angles of faces relative to the world, and the player face touched/hit

I need quite an unusual script that can determine the angle of any face of any object relative to the world, and be able to calculate which face was touched by the player after a minimum un-grounded velocity. thanks to anyone who helps.

note - i am extremely new to unity, but i know plenty of java and c++.

When you say face do you mean triangle in a mesh or are you talking more of a cube’s face (which might include more than one triangle)?

If you’re talking triangles check out Unity - Scripting API: RaycastHit.triangleIndex
This will return the triangle you’ve selected. Using this info you then find the 3 vertexes that comprise this triangle get their normals, average them to get an overall triangle normal which I think is what your looking for.

For a cubes face it might be easiest to setup a few plane colliders which of course you’d then know the facing as it would be one collider per face kind of idea.

but i need to be able to tell the angle of any face… let’s say i wanted to make a raycast from my feet to out about a small distance. and i wanted to make a ratio based on the player velocity and angle of the face of any object relative to the world(lets say it was a terrain patch), and then if the ratio is high enough, reverse the player velocity.

That’s what normals (as BPPHarv said) are for. The normal vector points in the “outward” direction for each vertex (as Unity stores vertex normals), the “outward” direction for the face should be the average of the vertex normals. You can then use that to find the angle of the face relative to whatever vector you want.

i honestly have no idea what im doing


#pragma strict
private var playerController : CharacterController;
function Start () {
playerController = GetComponent(CharacterController);
}
var rayStartPos : Vector3;

function Update () {

var fwd = transform.TransformDirection (Vector3.forward);
rayStartPos.x=playerController.center.x;
rayStartPos.y=playerController.center.y-5;
rayStartPos.z=playerController.center.z;
var hit: RaycastHit;
var ray = Physics.Raycast(rayStartPos, fwd, 30);

Debug.DrawRay(playerController.center, hit.point, Color.green);
print(rayStartPos.x);
}

The code you’ve posted would be like the very first step that you’d need to do.
Get a ray from your character to some point (if any).

Using the returned hit.normal (which is a vector pointing in the direction the triangle hit is facing) you then can calculate that angle. I think you can use Vector3.Angle(hit.normal, fwd); to find the angle between the two.

Something to that effect at least. The last time I did it, I implemented in a cross / dot product mess instead of Vector3.Angle but I think they worked out to be the same number in the end.

Also when posting code on the forums use code tags. There’s a sticky thread on the forums explaining how to post code segments at http://forum.unity3d.com/threads/143875-Using-code-tags-properly