Extrude 2D Mesh

Hi, I’m making a kind of game where I have to move a character in a 2D ground, like that:

I generated my map with some methods:

void ConstructMesh()
    {
        for (int mx = 0; mx < 60; mx++)
        {
            for (int my = 0; my < 30; my++)
            {
                BuildCollider (mx, my);

                if (GetBlockType (mx, my) == (int)MAP_TYPE.GRASS)
                    BuildMesh (mx, my, Grass);
                else if (GetBlockType (mx, my) == (int)MAP_TYPE.STONE)
                    BuildMesh (mx, my, Stone);
            }
        }
    }

void BuildMesh(float x, float y, Vector2 texture)
    {
        m_Vertices.Add (new Vector3 (x, y, 0));
        m_Vertices.Add (new Vector3 (x + 1, y, 0));
        m_Vertices.Add (new Vector3 (x + 1, y -1, 0));
        m_Vertices.Add (new Vector3 (x, y -1, 0));

        m_Triangle.Add (faceCount * 4);
        m_Triangle.Add (faceCount * 4 + 1);
        m_Triangle.Add (faceCount * 4 + 3);
        m_Triangle.Add (faceCount * 4 + 1);
        m_Triangle.Add (faceCount * 4 + 2);
        m_Triangle.Add (faceCount * 4 + 3);
        faceCount++;

        m_UV.Add (new Vector2 (texture.x * m_UnitTexture, texture.y * m_UnitTexture + m_UnitTexture));
        m_UV.Add (new Vector2 (texture.x * m_UnitTexture + m_UnitTexture, texture.y * m_UnitTexture + m_UnitTexture));
        m_UV.Add (new Vector2 (texture.x * m_UnitTexture + m_UnitTexture, texture.y * m_UnitTexture));
        m_UV.Add (new Vector2 (texture.x * m_UnitTexture, texture.y * m_UnitTexture));
    }

void GenerateMesh()
    {
        Mesh colMesh = new Mesh ();
        colMesh.vertices = m_ColVertices.ToArray ();
        colMesh.triangles = m_ColTriangle.ToArray ();
        m_ColRef.sharedMesh = colMesh;

        m_ColVertices.Clear ();
        m_ColTriangle.Clear ();
        colliderCount = 0;

        m_MeshRef.vertices = m_Vertices.ToArray ();
        m_MeshRef.triangles = m_Triangle.ToArray ();
        m_MeshRef.uv = m_UV.ToArray();
                m_MeshRef.RecalculateNormals ();

        m_Vertices.Clear ();
        m_Triangle.Clear ();
        m_UV.Clear ();
        faceCount = 0;
    }

But I don’t know how I can “remove” specific parts of the mesh where I move the character.
I tried to delete triangles and recalculate the normals, but it didn’t work…

I searched on Internet but I didn’t found how to do it.
Thanks for your help.

If you’re really making a game like Dig Dug, it’d be much easier (and more flexible) to do it using PixelSurface than trying to dynamically modify a mesh. Check out the official forum thread here.

Disclaimer: I made PixelSurface, so I may be considered biased. But this sort of problem is exactly why I made it! :slight_smile:

In ConstructMesh function check if the the tile you’re currently at is empty.
If it is, don’t call BuildCollider and BuildMesh function for it.
You can have a class for empty block or simply use null.

To remove block, set the element of array at its position to empty tile and call ConstructMesh function again.
It should regenerate your mesh from scratch, using modified array.

This tutorial should help you, some parts of it are outdated, but you will get the concept:
Tutorial

1 Like