Making empty spaces unwalkable in 2D Physics Grid Graph


I’m using 2D Grid Graph with 2D physics, and I was wondering how you’d make empty spaces where there’s no collision be unwalkable:

As you can see, the inside of the walls are walkable, the sides and the walls are not walkable, but the areas outside of the walls are walkable. They should be unwalkable instead.

I would use Height Testing but my game uses 2D physics so that’s impossible.

@aron_granberg said in another thread you can make the default be unwalkable and change the Obstacle layer mask to being walkable mask (instead of it being unwalkable mask) but the changes he said to make to do that doesn’t work anymore.

What’s the simplest solution?



So in your image, you simply want to change all walkable nodes to unwalkable, and all unwalkable nodes to walkable?

@aron_granberg not quite. Ideally I want height testing with 2D physics, but that isn’t possible. The blueish area outside of the walls don’t have any sprites or anything in those nodes, so they are meant to be unwalkable.

I figured one solution is to make everything unwalkable by default Except for the layers I assign to be walkable (the reverse of the default behaviour where everything is walkable except for obstacle layers)

In my image, the walls and outside the walls are meant to be unwalkable, and only the ground nodes are walkable, so simply flipping walkable to unwalkable and vice versa wouldn’t work.

Hmm maybe I should be a bit more general with my question. I got a 2D map that looks like this:

It’s a non-square/rectangular shaped map, and only the Ground areas should be walkable. The grid graphs can only be square/rectangular shaped, so I have to stretch a grid graph from top to bottom covering the whole map, which includes empty spaces, like this:

Which is a problem as you can see, the ground is walkable, which is good and the walls have colliders and added to the obstacle layer mask, so those are fine. But the empty spaces outside of the walls are also walkable, which is wrong.

How best to approach this problem?



Ah. I see. Well. This is not possible out of the box unfortunately, you will need to modify/subclass the grid graph and update the RecalculateCell method.
You could also just modify the grid graph from a separate script which might be easier. See the example on this page:

1 Like