Hi,
I am trying to raycast from the centre of a mesh triangle in the direction that the triangle is facing.
I am able to get the middle(average) position of the triangle as the starting point of the raycast, but can’t seem get the last bit, and no matter what I try, I just can’t get it. Below is cleaned up code showing the triangle info and average position.
//Triangle Average positions
for (var a : int = 0; a < triangles.Count; a++)
{
var averagePosition = ((triangles[a].tri0 + triangles[a].tri1 + triangles[a].tri2) / 3);
trianglesAveragePosition.Add(averagePosition);
}
Any ideas out there? Thanks!
Given three unique vertices, you can construct two vectors and take the cross produce. So if a, b, and c are three vertices, then it will be something like:
var v1 = b - a;
var v2 = c - b;
var normal = Vector3.Cross(v1, v2);
Note if a, b, & c are taken directly from the mesh, they will be in local coordinates, so you will have to do a ‘transform.TransformDirection()’ to get the world direction of the normal. In addition, I don’t remember the direction of the winding of the vertices in Unity triangles, so you may have to reverse the order of the parameters in the Vector3.Cross().