Trying to delete a triangle from a mesh

Hello, i am trying to delete a triangle from my mesh. My mesh is consisted by 3 submeshes, each one is inside the other. At first I use Raycast to get the hit.triangleIndex. Then i am manipulating the vertices. I want to delete the triangle that is intersecting my inner mesh. I dont know if its the right way, but i am using raycast from each vertex against the inner mesh.

public void RayCastDeform(int triangleIndex)
    {
    

        Vector3 p0 = vertices[triangles[triangleIndex * 3 + 0]];
        Vector3 p1 = vertices[triangles[triangleIndex * 3 + 1]];
        Vector3 p2 = vertices[triangles[triangleIndex * 3 + 2]];

        var dir0 = dirs[triangles[triangleIndex * 3 + 0]];
        var dir1 = dirs[triangles[triangleIndex * 3 + 1]];
        var dir2 = dirs[triangles[triangleIndex * 3 + 2]];


        p0 += dir0 * 0.1f * Time.fixedDeltaTime;
        p1 += dir1 * 0.1f * Time.fixedDeltaTime;
        p2 += dir2 * 0.1f * Time.fixedDeltaTime;


        vertices[triangles[triangleIndex * 3 + 0]] = p0;
        vertices[triangles[triangleIndex * 3 + 1]] = p1;
        vertices[triangles[triangleIndex * 3 + 2]] = p2;


        //Returns true if the vertex pointing at the collisionBitMask layer
        var p0isPointingAtBitMask = Physics.Raycast(transform.TransformPoint(p0), transform.TransformDirection(dir0), math.INFINITY, collisionBitMask);
        var p1isPointingAtBitMask = Physics.Raycast(transform.TransformPoint(p1), transform.TransformDirection(dir1), math.INFINITY, collisionBitMask);
        var p2isPointingAtBitMask = Physics.Raycast(transform.TransformPoint(p2), transform.TransformDirection(dir2), math.INFINITY, collisionBitMask);

        //If all vertices are not pointing at collisionBitMask layer, delete the triangle
        if (!p0isPointingAtBitMask && !p1isPointingAtBitMask && !p2isPointingAtBitMask )
        {
            Debug.Log("Triangle crossed the Rock_Layer!");

            triangles.RemoveRange(triangleIndex*3, 3);
        }
        else
        {
            Debug.Log("Triangle crossed NOT the Rock_Layer!");
        }
}

The thing is that is not working correctly.
I am open to suggestions for something better than this.

Staring at the code is only going to get you so far. To debug the above, strip the dataset down so it is the tiniest possible, which in this case might be three triangles, one in each of your three submeshes.

That way you can use Debug.Log() on EVERY part of the steps above and find out where it is going wrong.

You probably also want to break up lines 25 to 27 so you can reason about the parts going into the raycasts and the results coming out of them.

Make sure your code doesn’t rely on submesh order, which I find can sometimes vary from model to model in very counter-intuitive ways.

Whatever it is, you must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

Thanks for your reply and the informations Kurt-Dekker. I used Debug.Log() statements on my code, i just didnt including. I also draw lines between my points to check if i manipulate the right triangle and it seems fine. The weird thing is that my raycasts are not executing every frame i think, because i dont get many log ouputs from my if statement. Btw i am aiming for pc.

Well when are you calling it? The above function is never directly called by Unity.

Keep in mind that if you change the mesh on the MeshFilter, it does not (necessarily) update what is on the MeshCollider. The MeshCollider is what matters for Raycast of course. You may have to supply the modified mesh to the MeshCollider if you are iterating this repeatedly.

I am aware of these. I am calling this method from Update() of an other script and i am updating the meshfilter, meshcollider on FixedUpdate() if a modification happens(using a boolean).

If you call it in Update, it is getting called every frame. If it is not getting called every frame, you call it under a condition that is not true every frame. Check that, if it might be related to your problem

Also, FixedUpdate() is for physics only and mesh alterations are inherently expensive. Unless you have a very good reason to do this in FixedUpdate, dont. Slowing down FixedUpdate causes exponential slowdown to your game, since Unity does its best to call it every fixed timestep - which might be multiple times per frame or once every couple frames. The longer FixedUpdate takes, the less normal frames you can calculate before the next FixedUpdate must happen. In theory, if FixedUpdate approaches or takes longer than the fixed timestep, Unity would only ever calculate FixedUpdate ever again. In practice, Unity detect that and renders a normal Update here and there, but the problem remains.
Slowing down Update slows down linearly. Slowing down FixedUpdate slows down exponentially.

Helpful informations Yoreki, thanks. Thought i didnt accomplish to delete the right triangles. I even used a simple mesh for testing and its removing some random triangles. Maybe is sth wrong with my logic ? Is there a better way to remove geometry that intersects my other mesh at runtime?