Raycast doesn't interact with Tilemap Collider when in a 3D scene.

So, I’m making a grid based game that the player views from a first person perspective. I’ve decided to use the Tilemap system in a 3D space to make level creation easy, but I’ve run into a little snag. For some reason, all of my attempts to detect the walls in my game haven’t worked.

I’m using the Tilemap collider as an easy way to detect collisions with immovable objects however, I’m unable to get my Raycast to interact with them. My Raycast just never reacts to the collider.

I’ve made sure that the Raycast was getting fired from the correct location, and I’ve even made sure it was firing in the right direction. I even tried firing the Raycast in many different directions just to make sure I had the direction right, and none of them hit anything.

Although, I did try placing a cube below the floor to see if the raycast could see it, and it did! That means that the Raycast is ignoring the Tilemap collider while still interacting with other objects.

So my question is this, am I doing something wrong, or were Raycasts just never designed to work with the Tilemap collider system while in a 3D space?

If it’s the former, I’d highly appreciate some guidance into how I might fix this problem. It it happens to be the latter, could someone please help me with figuring out an alternate solution?

Incase it helps, I’ll post the code that I’m using for the Raycast below,

            RaycastHit ColInfo;
            CollisionCheck.transform.Translate(MoveDir.x, MoveDir.y, 0);
            if (Physics.Raycast(CollisionCheck.transform.position, Vector3.forward, out ColInfo))
                //Something is ahead of the player, so interact with it! (Currently it's just walls, so it'll just stop player movement.)
                IsBusy = false;

If there’s anything else you need to know to help, I’ll happily answer any questions.

The main issue I had when trying to solve the same problem was that when my main Camera was in Perspective projection mode, my Tilemap wasn’t registering a hit so, I couldn’t get the distance value I needed for ScreenToWorldPoint. The key to solving this for me was… Tilemap.localBounds.IntersectRay(ray, out float distance); This is different than a Raycast but, it gives you the distance you need from where the ray intersects with the Tilemap.

Below is the code I used for both Perspective and Orthographic projections in 3D mode. I stole some of my comments from Unity’s API documentation. Also, it’s possible that some of the terminology is off since I’m new to much of this.

// -------------------------
//   Perspective Approach 
// -------------------------

Ray ray;
bool hitSomething; 

// Step 1: Get the mouse click position (where it was when you clicked)

Vector3 mousePos = Input.mousePosition;
// Step 2: Returns a ray going from camera through a screen point.
//             Resulting ray is in world space, starting on the near plane 
//             of the camera and going through position's (x,y) pixel 
//             coordinates on the screen (position.z is ignored).

ray = Camera.main.ScreenPointToRay(mousePos);

// Step 3: Once you have the ray you need to know the distance in order
//              to get the position in world space. 

hitSomething = tiles.localBounds.IntersectRay(ray, out float distance);

// Step 4: The worldspace point created by converting the screen
//             space point at the provided distance z from the camera plane.

Vector3 screenSpace = new Vector3(mousePos.x, mousePos.y, distance);
Vector3 worldSpace = Camera.main.ScreenToWorldPoint(screenSpace);

// Step 5: Convert the worldSpace point to the cell coordinates of the Tilemap (which is named 'tiles')

Vector3Int cellLocation = tiles.WorldToCell(worldSpace);

// -------------------------
//   Orthographic Approach 
// -------------------------

Vector3 screenSpace = new Vector3(Input.mousePosition.x, 
                        Input.mousePosition.y, Camera.main.transform.position.z);

Vector3 worldSpace = Camera.main.ScreenToWorldPoint(screenSpace);
Vector3Int cellLocation = tiles.WorldToCell(worldSpace);