- Home /
Keep player inside a non-square/irregular shape area
I'm building a top-down twin-stick shooter with a level editor.
For gameplay reasons, the level is made of tiles, and the arena can be any shape whatsoever. For example (x representing a tile):
xxxxxxxxxx
xxxx xxxx
xxxx xxxx
xxxxxxxxxx
xx
xxxxxxxxxx
xxxxxxxxxx
The player can move freely inside the arena, but can't leave it. I was wondering how to check for collisions and make sure the player can't leave.
Any ideas?
I guess that you can easily find 4 points which are top, bottom, left, right from your matrix, and from that, you can create a collider to prevent your player leaves that box.
Answer by kento14 · Jan 07, 2017 at 04:59 PM
I would make an empty gameObject with a 2d BoxCollider and place it in the empty area (so it fills the area without the x's). And for non-square areas, I would use a polygon collider 2D.
This will do so when the player comes to an empty tile (no x) the boxCollider/PolygonCollider 2D, will stop the player;