RaycastHit.triangleIndex

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.

JavaScript
    // Attach this script to a camera and it will
// draw a debug line triangle triangle
// at the triangle where you place the mouse.
function Update () {
// Only if we hit something, do we continue
var hit : RaycastHit;
if (!Physics.Raycast (camera.ScreenPointToRay(Input.mousePosition), hit))
return;
// Just in case, also make sure the collider also has a renderer
// material and texture
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;
// Extract local space vertices that were hit
var p0 = vertices[triangles[hit.triangleIndex * 3 + 0]];
var p1 = vertices[triangles[hit.triangleIndex * 3 + 1]];
var p2 = vertices[triangles[hit.triangleIndex * 3 + 2]];
// Transform local space vertices to world space
var hitTransform : Transform = hit.collider.transform;
p0 = hitTransform.TransformPoint(p0);
p1 = hitTransform.TransformPoint(p1);
p2 = hitTransform.TransformPoint(p2);
// Display with Debug.DrawLine
Debug.DrawLine(p0, p1);
Debug.DrawLine(p1, p2);
Debug.DrawLine(p2, p0);
}

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
void Update() {
RaycastHit hit;
if (!Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), out hit))
return;

MeshCollider meshCollider = hit.collider as MeshCollider;
if (meshCollider == null || meshCollider.sharedMesh == null)
return;

Mesh mesh = meshCollider.sharedMesh;
Vector3[] vertices = mesh.vertices;
int[] triangles = mesh.triangles;
Vector3 p0 = vertices[triangles[hit.triangleIndex * 3 + 0]];
Vector3 p1 = vertices[triangles[hit.triangleIndex * 3 + 1]];
Vector3 p2 = vertices[triangles[hit.triangleIndex * 3 + 2]];
Transform hitTransform = 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);
}
}

import UnityEngine
import System.Collections

class example(MonoBehaviour):

def Update():
hit as RaycastHit
if not Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), hit):
return
meshCollider as MeshCollider = hit.collider
if (meshCollider == null) or (meshCollider.sharedMesh == null):
return
mesh as Mesh = meshCollider.sharedMesh
vertices as (Vector3) = mesh.vertices
triangles as (int) = mesh.triangles
p0 as Vector3 = vertices[triangles[((hit.triangleIndex * 3) + 0)]]
p1 as Vector3 = vertices[triangles[((hit.triangleIndex * 3) + 1)]]
p2 as Vector3 = vertices[triangles[((hit.triangleIndex * 3) + 2)]]
hitTransform as 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.