LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

RaycastHit.triangleIndex

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public var triangleIndex: int;
public int triangleIndex;

Description

The index of the triangle that was hit.

Triangle index is only valid if the collider that was hit is a MeshCollider.

#pragma strict
// This script draws a debug line around mesh triangles 
// as you move the mouse over them.
var camera: Camera;
function Start() {
	camera = GetComponent.<Camera>();
}
function Update() {
	var hit: RaycastHit;
	if (!Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), hit))return ;
	var meshCollider: MeshCollider = hit.collider as MeshCollider;
	if (meshCollider == null || meshCollider.sharedMesh == null)return ;
	var mesh: Mesh = meshCollider.sharedMesh;
	var vertices: Vector3[] = mesh.vertices;
	var triangles: int[] = mesh.triangles;
	var p0: Vector3 = vertices[triangles[hit.triangleIndex * 3 + 0]];
	var p1: Vector3 = vertices[triangles[hit.triangleIndex * 3 + 1]];
	var p2: Vector3 = 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);
}
// This script draws a debug line around mesh triangles 
// as you move the mouse over them.
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { Camera camera; void Start() { camera = GetComponent<Camera>(); } 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); } }