Hi everyone,
i’m new with unity and i’m programming a lot for learn but now i encountered a problem.
I need to get the Triangle Index of specific mesh (in specific layer) for destroiyng it when a player walking on.
i Try this code and Attach to the mesh i want to “destroy” but when the condition is triggered the deleted Triangles are not correct.
What i do is start collect the position of character at colision enter and stop it at collision exit then i need to destroy all the triangles in the collected position.
First code is very simple:
I set a condition for update
void OnCollisionEnter(Collision collision)
{
entered= true;
}
After that in te Update function if entered is true i collect every Player Vector3 position:
if (entered)
{
if(last_position!= player.transform.position)
{
last_position= player.transform.position;
position.Add(last_position);
}
}
Now in the exit collision event i do this:
entered= false;
int layerMask = 1 << 8;
layerMask = ~layerMask;
int j = 0;
RaycastHit hit;
Ray ray;
while (j < position.Count)
{
if (Physics.Raycast(position[j], Vector3.down, out hit, 100,layerMask))
{
delete_triangle(hit.triangleIndex);
}
}
position.Clear();
And this is the delete_triangle function where i receive an error “IndexOutOfRangeException: Index was outside the bounds of the array” at the below marked line and i notice that the Triangle Index is always -1
Destroy(this.gameObject.GetComponent<MeshCollider>());
mesh = GetComponent<MeshFilter>().mesh;
int[] old_trangle = mesh.triangles;
int[] new_triangle = new int[mesh.triangles.Length - 3];
int i = 0;
int j = 0;
while (j < old_trangle.Length)
{
if (j != triangle_index * 3)
{
[COLOR=#ff0000] new_triangle [i++] = old_trangle[j++];[/COLOR]
new_triangle [i++] = old_trangle[j++];
new_triangle [i++] = old_trangle[j++];
}
else
{
j += 3;
}
}
this.gameObject.AddComponent<MeshCollider>();
transform.GetComponent<MeshFilter>().mesh.triangles = new_triangle ;
Thank you in advance for your help