var triangleIndex : int
Description
The index of the triangle that was hit.
Triangle index is only valid if the collider that was hit is a MeshCollider.
function Update () {
var hit : RaycastHit;
if (!
Physics.Raycast (camera.ScreenPointToRay(
Input.mousePosition), hit))
return;
var meshCollider = hit.collider as
MeshCollider;
if (meshCollider == null || meshCollider.sharedMesh == null)
return;
var mesh :
Mesh = meshCollider.sharedMesh;
var vertices = mesh.vertices;
var triangles = mesh.triangles;
var p0 = vertices[triangles[hit.triangleIndex * 3 + 0]];
var p1 = vertices[triangles[hit.triangleIndex * 3 + 1]];
var p2 = vertices[triangles[hit.triangleIndex * 3 + 2]];
var hitTransform :
Transform = hit.collider.transform;
p0 = hitTransform.TransformPoint(p0);
p1 = hitTransform.TransformPoint(p1);
p2 = hitTransform.TransformPoint(p2);
Debug.DrawLine(p0, p1);
Debug.DrawLine(p1, p2);
Debug.DrawLine(p2, p0);
}
See Also: Physics.Raycast, Physics.Linecast, Physics.RaycastAll.