Less error prone way to detect obstacles?

The technique for detecting obstacles seems straight forward but error prone. I’m casting a ray out of the center of the grid and my first thought is what happens if the wall isn’t centered on the grid.

Is this common practice in unity or is there a better technique? I know we bake navmeshes in many instances, which I guess do more high fidelity checking of the shape of obstacles so I’m wondering if there’s a better technique here someone might be aware of?

I’ve not done this before but I’m wondering if something like adding a collider to the GridSystemVisualSquarePrefab that we check for collisions with obstacles on start and set the collider active false after the check? Perhaps doing a higher sample raycast like 3 or 5 rays per X and Z axis? Maybe setting up some kind of snap to obstacle objects so it always intersects with the square center?

Maybe this doesn’t matter and I’m overthinking it but I can imagine this being a problem.

1 Like

Using raycasting from the center of the grid is a common technique, but you’re right in considering potential issues. Adding colliders to the GridSystemVisualSquarePrefab and checking for collisions on start could be a reasonable approach. Additionally, using multiple rays per axis or implementing a snap to obstacle objects can improve accuracy. You should consider experimenting with these methods to find the most effective solution for your specific case. Overthinking is okay – it often leads to more robust implementations.

Hope this helps!

Thanks, that’s helpful. I’ll probably go with multiple raycasts per square as it’s in line with the code already.

1 Like

You should probably use a box cast instead of multiple rays. With a box cast you can cast a box that’s the same size as the grid cell and then detect an obstacle regardless of it’s position on the cell.

See Physics.BoxCast here:


Thank you! I didn’t know about BoxCast before.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms