- Home /
Why RaycastHit.triangleIndex return -1 ?
I added a empty GameObject and added to it a Mesh Filter and Mesh Renderer components. And attached to it two scripts.
The first script create a mesh renderer plane.
using UnityEngine;
using System.Collections;
public class DragAndDrop : MonoBehaviour
{
public float width = 5f;
public float height = 5f;
private Vector3[] vertices = new Vector3[4];
private Mesh mesh;
void Start()
{
MeshFilter mf = GetComponent<MeshFilter>();
mesh = new Mesh();
mf.mesh = mesh;
int[] tri = new int[6];
tri[0] = 0;
tri[1] = 2;
tri[2] = 1;
tri[3] = 2;
tri[4] = 3;
tri[5] = 1;
// Normals (Only if you want to display the object in the game)
Vector3[] normals = new Vector3[4];
normals[0] = Vector3.forward;
normals[1] = Vector3.forward;
normals[2] = Vector3.forward;
normals[3] = Vector3.forward;
// UVs (How textures are displayed)
Vector2[] UV = new Vector2[4];
UV[0] = new Vector2(0, 0);
UV[1] = new Vector2(1, 0);
UV[2] = new Vector2(0, 1);
UV[3] = new Vector2(1, 1);
// Assgin Arrays!
mesh.vertices = vertices;
mesh.triangles = tri;
mesh.normals = normals;
mesh.uv = UV;
}
void Update()
{
vertices = new[] { new Vector3(0, 0, 0), new Vector3(width, 0, 0), new Vector3(0, height, 0), new Vector3(width, height, 0) };
mesh.vertices = vertices;
}
}
The second script should cut holes in the Plane using the mouse button.
using UnityEngine;
using System.Collections;
public class CutHole : MonoBehaviour {
// Use this for initialization
void Start () {
}
void deleteTri(int index)
{
Destroy(gameObject.GetComponent<MeshCollider>());
Mesh mesh = transform.GetComponent<MeshFilter>().mesh;
int[] oldTriangles = mesh.triangles;
int[] newTriangles = new int[mesh.triangles.Length - 3];
int i = 0;
int j = 0;
while(j < mesh.triangles.Length)
{
if(j != index*3)
{
newTriangles[i++] = oldTriangles[j++];
newTriangles[i++] = oldTriangles[j++];
newTriangles[i++] = oldTriangles[j++];
}
else
{
j += 3;
}
}
transform.GetComponent<MeshFilter>().mesh.triangles = newTriangles;
gameObject.AddComponent<MeshCollider>();
}
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit, 1000.0f))
{
deleteTri(hit.triangleIndex);
}
}
}
}
I'm using a break point on the line:
deleteTri(hit.triangleIndex);
And triangleIndex is always -1
And another break point on the line
newTriangles[i++] = oldTriangles[j++];
This throw the exception:
IndexOutOfRangeException: Array index is out of range. CutHole.deleteTri (Int32 index) (at Assets/MyScripts/CutHole.cs:26) CutHole.Update () (at Assets/MyScripts/CutHole.cs:82)
Answer by CasperK · Nov 29, 2016 at 11:02 AM
Do you also have a Mesh Collider on the GameObject?
From the RaycastHit.triangleIndex documentation:
Triangle index is only valid if the collider that was hit is a MeshCollider.
I tried now to add a $$anonymous$$esh Collider to the gameobject but once i click with the mouse on the plane the mesh collider is gone like removed automatic from the inspector. The mesh collider component is removed once i click with the mouse on the plane and i'm getting the same exception and it's still -1.
But when i stop the game the mesh collider is back on the inspector it's gone only when running the game and clicking the plane. Else if the game is not running the mesh collider is in the inspector.
Should i change something in the mesh collider properties ?
I've been trying some things and it seems the adding of a mesh on runtime has changed over the years.
I noticed it worked when you'd enable/disable the collider in the inspector.
It's not the nicest fix but you can disable the collider after setting the mesh and enable it again after a milisecond. So from this:
mesh.vertices = vertices;
mesh.triangles = tri;
mesh.normals = normals;
mesh.uv = UV;
}
To something like this:
mesh.vertices = vertices;
mesh.triangles = tri;
mesh.normals = normals;
mesh.uv = UV;
GetComponent<$$anonymous$$eshCollider>().shared$$anonymous$$esh = mesh;
GetComponent<$$anonymous$$eshCollider>().enabled = false;
Invoke("Enable$$anonymous$$eshCollider", .001f);
}
void Enable$$anonymous$$eshCollider()
{
GetComponent<$$anonymous$$eshCollider>().enabled = true;
}
This is working.
Last question: If i want to create a Plane like in this video ? In fact i tried to do it using this video and trying to understand it.
The Plane there is built from many triangles so when you click on them it's making holes in it. In my Plane script it's creating a Plane built only of two triangles. So when i click on them it's just deleting the Plane.
This is what i mean a Plane with many triangles:
Your answer
Follow this Question
Related Questions
How can I animate linerenderer lines over time ? 1 Answer
How do i use a public sealed class to create objects and destroy them ? 0 Answers
How can I call the Load method and/or the ShootingSettings method also only once in the Update ? 1 Answer
How can i check and fire an event when the user look at specific object ? 0 Answers