Maze collider

Hi everyone, This is my first question in this forum.

I am creating a game where a player has to go through a maze, for this I need the player to not be able to logically go through the walls and that is my question.

How can I achieve this?

  • The maze is a PNG image and it is a complex maze, which is why Polygon Collider 2D is not a good option and Box Collider neither.
  • I have also tried through the Sprite editor using Physics Shape but it is not debugged either.
  • Another option I tried was to create a script based on the color of the pixels of the maze and it didn’t work either

I have to say that I’m still learning.

Does anyone have a good idea?

Thank you

3 Answers

3

There are many things how you could build a maze. You could, for example, use Tiles and Tile editor, but I don’t have that much experience with that either.
If you really want to do use the PNG as source, you could import it as sprite, with the walkable area transparent, and let unity generate a PolygonCollider2D from that automatically. Of coruse, the collider only serves as collision detection, so then you’d either need to use rigidbody to move your character to prevent it from walking from walls, or query the polygon yourself by something like Physics.OverlapSphere. You could as well just read a corresponding pixel from the texture to tell you if there’s a wall or not, but that depends on the physics of your motion - you might want to bounce or slide on the wall edges, and colliders + rigidbody make this easy for you.
Another approach would be to use the generated colliders to generate a NavMesh surface on which you could then walk. That would also give you an ability to find the path through the maze using the existing tools.

Now you’ve mentioned some of these approaches, and I wonder what made you think it’s not viable or it didn’t work. Feel free to elaborate more, share what you’ve tried, or describe in more detail what would you like to achieve.


Polygon2D collider generated from sprite using Sprite Editor

Thanks for the help, but what happens with the polygon collider 2d is that it does not fit the surface of my maze, blocking places where the player should be able to pass. The player has a rigdoby2D and a 2d circle collider. Here I leave the Sprite that I want to use.

I have added a screenshot of polygon collider to my answer as I can't add screenshots to comments.

–

Thank you si much, It looks really nice, i will try It Tomorrow and left you know. Really I appreciate it

–

I think the suggestion from @Pangamini to use a Tilemap with a Tilemap Collider is the simplest option. This will require you to manually trace over the PNG with tiles to define the collision area. You can turn off the Tilemap Renderer so that only your PNG is shown.