- Home /
 
Creating 2d image map of objects based off 3d space colliders Unity C#
I have been trying the last few days to generate a .jpeg/.png image colour tile map based off of a unity scene where it is mapping objects with the layer "block", anything that it collides with including inside of, gets marked in a colour. Anything it doesnt is marked in black. Imagine a 1025 grid with the below format as an example, an "x" is a area with an object / series of objects with the layer as "block", and a mesh collider.
 xxx             x                x
 xxx                      xxx        x
 xxx      xx              xxx
 
               Assuming everything has the y value 0, I have been using two for loops to iterate through space x and z and I have tried raycasting 1 point forwards which works, except it wont map the inside of objects. I cant get a cube with a collider to generate the map either, OnCollisionEnter doesnt trigger.
What I am aiming for is ideally using a point of scale 1,1,1 to iterate through each point on the map using the for loops and saying basically I am inside of a collider with tag or not inside collider and it moves like so
1 - inside collider
 0xx             x                x
 xxx                      xxx        x
 xxx      xx              xxx
 
               2 - inside collider
 x0x             x                x
 xxx                      xxx        x
 xxx      xx              xxx
 
               3 - inside collider
 xx0             x                x
 xxx                      xxx        x
 xxx      xx              xxx
 
               4 - not inside collider
 xxx0            x                x
 xxx                      xxx        x
 xxx      xx              xxx
 
               For the raycasting, it almost works fine except for mapping the inside of objects, and it seems to have weird clipping around objects where its placing ray collisions a few points away from the object mesh colliders.
I created the mapMarker game object to visually display where the rays were colliding to help my debugging, and it is on the edges of objects. Which is how i discovered the weird clipping where its a good few points out.
  for (float i = 0; i < 1025; i++)
     {
         for (float j = 0; j < 1025; j++)
         {
             height = getHeightOfTerrain(j, i);
     
             // -1 as we are casting 1 point forward
             Ray r = new Ray(new Vector3(j-1, height, i), new Vector3(1, 0, 0)); 
             RaycastHit hit;
             // if ray hits collider with the layer mask
             if (Physics.Raycast(r, out hit, 1, LayerMask.GetMask("block")))
             {
                 obj = hit.transform.gameObject;
                 // add the colour to the 2d map
                 texture.SetPixel((int)hit.point.x, (int)hit.point.z, new Color(255, 0, 0));
                 GameObject mapMarker = GameObject.CreatePrimitive(PrimitiveType.Cube);
                 mapMarker.name = obj.name;
                 
                 mapMarker.GetComponent<Renderer>().material.color = new Color(255, 0, 0); 
                 mapMarker.transform.position = new Vector3(hit.point.x + 1f, height + 2f, hit.point.z);
             }
             else
             {
                 texture.SetPixel((int)j, (int)i, new Color(0, 0, 0));
             }
         }
     }
 
               How would you approach this? Is my method just wrong? I tried iterating a box collider through the above code and doing collision tests on the box collider but it never triggered anything.
Your answer