Collider not being applied across generated map

Hey guys!

My colliders aren’t properly being placed on my tilemap (What I mean is the coordinate system of squares that will be the basis of my game)…

If my player is on top of the collider it won’t fall through as expected, but only when on top of the closest portion of the map with the green “plane” mesh.

So basically, it looks like my collider is only being applied to the first square of my map and not all of them?

If I disable the mesh and set it to none…

Here are my player components:

Here’s the script that generates my TileMap:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshCollider))]
[ExecuteInEditMode]
public class TileMap : MonoBehaviour
{
    public int sizeX = 100;
    public int sizeZ = 100;
    public float tileSize = 1.0f;

    void Start ()
    {
        BuildMesh();
    }
  
    public void BuildMesh()
    {
        /**factor in size of the map and calculate the number of vertices and triangles. A 1x1 coordinate square system is
         * made up of 2 triangles, for a total of 4 vertices per square.
         **/
        int numTiles = sizeX * sizeZ;
        int numTriangles = numTiles * 2;
        int verticeSizeX = sizeX + 1;
        int verticeSizeZ = sizeZ + 1;
        int numVertices = verticeSizeX * verticeSizeZ;

        //Configure data structures to hold the associated information for the TileMap
        Vector3[] vertices = new Vector3[numVertices];
        Vector3[] normals = new Vector3[numVertices];
        Vector2[] uv = new Vector2[numVertices];
        int[] triangles = new int[numTriangles * 3]; //for each sqaure: 2 triangles, each with 3 points

        //Generation (in 2 passes)
        int x, z;
        for (z = 0; z < verticeSizeZ; z++)
        {
            for (x = 0; x < verticeSizeX; x++)
            {
                vertices[z * verticeSizeX + x] = new Vector3(x * tileSize, 0, z * tileSize);
                normals[z * verticeSizeX + x] = Vector3.up;
                uv[z * verticeSizeX + x] = new Vector2((float)x / verticeSizeX, (float) z / verticeSizeZ);
            }
        }

        for (z = 0; z < sizeZ; z++)
        {
            for (x = 0; x < sizeX; x++)
            {
                int squareIndex = z * sizeX + x;
                int triOffset = squareIndex * 6;

                triangles[triOffset + 0] = z * verticeSizeX + x + 0;
                triangles[triOffset + 1] = z * verticeSizeX + x + verticeSizeX + 0;
                triangles[triOffset + 2] = z * verticeSizeX + x + verticeSizeX + 1;

                triangles[triOffset + 3] = z * verticeSizeX + x + 0;
                triangles[triOffset + 4] = z * verticeSizeX + x + verticeSizeX + 1;
                triangles[triOffset + 5] = z * verticeSizeX + x + 1;
            }
        }

        Mesh mesh = new Mesh();

        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mesh.normals = normals;
        mesh.uv = uv;

        //Preliminaries
        MeshFilter mf = GetComponent<MeshFilter>();
        MeshRenderer mesh_Renderer = GetComponent<MeshRenderer>();
        MeshFilter mesh_Filter = GetComponent<MeshFilter>();
        MeshCollider mesh_Collider = GetComponent<MeshCollider>();


        mesh_Filter.mesh = mesh;
        Debug.Log("Mesh Generated");

    }
}

What I do is I generate each square with 2 passes and I feel that my collider should be generated across this whole entire surface… But it only works in this one small corner of my map (the first square) as you can see by the pictures. How do I get my collider to apply to every single square and not just one?

Thanks :slight_smile:

Because the Mesh Collider takes in a mesh to begin with to “build” the collider for you. It might not be updated automatically if you alter the mesh.
Your screenshot shows a discrepancy between the mesh in the MeshFilter and the mesh in the MeshCollider
I’ve never tried this before, but maybe after you build the tiles, you can update the mesh collider:
GetComponent<MeshCollider>().sharedMesh = mesh; //Update the mesh collider with the new mesh
Or you might have to add the mesh collider dynamically after you build the tiles? I’ve never really used MeshColliders though.

THANK YOU SO MUCH! This worked :slight_smile: (edit: “GetComponent<MeshCollider>().sharedMesh = mesh;” was all I needed to do).