2
\$\begingroup\$

I'm making a top down 2D game in Unity, here's my doubt: how can I add "hole" tiles to it, so the player could fall (something like Zelda: Link to the past, or the water tiles in Enter The Gungeon)?

Example

The solution I thought is to put tags on each tile (ground, hole, trap, etc), put the player on Z = -5 and use the 3D Physics.Raycast to check in which tile the player is. But that doesn't seem optimized, I would need to spam colliders in every tile and use the Raycast at each frame. Can you guys give me better alternatives?

Thanks a lot! :)

\$\endgroup\$
5
  • 1
    \$\begingroup\$ If you're using a regular grid of tiles, say every tile is exactly 1 unit in width & height, then you can determine which tile the player is over by simply rounding their coordinates to an integer - boom, you have an index into a 2D array of tile properties, no raycast or colliders required. \$\endgroup\$
    – DMGregory
    Feb 5, 2017 at 23:58
  • \$\begingroup\$ @DMGregory Your solution sounds great, but for now I've only implemented a tile picker and a brush (I still don't have a matrix of tags that represents the map). Hmm, maybe I should use Tiled Map Editor to build the maps. \$\endgroup\$ Feb 6, 2017 at 0:10
  • 1
    \$\begingroup\$ That's a great start. Now, just add a MonoBehaviour or ScriptableObject that represents your map and, when your picker/brush change a tile, notify that script to update its tile array. ;) (Or, alternately, have a script that scans all the tiles at startup and builds its array from that) \$\endgroup\$
    – DMGregory
    Feb 6, 2017 at 0:13
  • \$\begingroup\$ @DMGregory It also sounds great, but let's say I started creating a map, then I decided it was too small, so I change the grid dimensions. It would break the connection between the tilemap in the scene and my 2D array. In that case I would check the X,Y coordinates of all tile objects in the scene and update my indexes in a new 2D array? \$\endgroup\$ Feb 6, 2017 at 0:23
  • 1
    \$\begingroup\$ That sounds like you already have a solution. ;) \$\endgroup\$
    – DMGregory
    Feb 6, 2017 at 3:18

3 Answers 3

1
\$\begingroup\$

I don't know what tool you are working with but as if you work in unity like me, you can make a trigger for those holes and certainly player has a collider. so in collision stay of hole or player just write a code that checks the tag of other. if the distance was less than a threshold write the code for falling.

\$\endgroup\$
2
  • \$\begingroup\$ Yes, I can put colliders in the hole tiles, but I need to be sure that the player fully left the ground tile, otherwise it would be frustrating for the player. \$\endgroup\$ Feb 5, 2017 at 23:58
  • 1
    \$\begingroup\$ @GuilhermeRecchiCardozo You can make the trigger zone smaller than the tile by half the size of the player from each edge. That is, if the tile is 100x100 pixels and the player is 16x16 pixels, you would reduce the collider from each edge by 8 pixels. The end-result would still be a reduction by 16, but centered. \$\endgroup\$ May 10, 2017 at 18:10
1
\$\begingroup\$

Think of the hole tiles as "teleporters" which teleport the player-character to a different map (the one representing the layer below it).

You will likely already have an implementation for these to implement doors and other gateways between maps. The only difference between a door of a building leading to an indoor map and a hole leading to the dungeon map of the floor below is the animation you play when the player-character steps into them.

Using 3d physics just for this gimmick is likely not worth the hassle.

\$\endgroup\$
0
\$\begingroup\$

If your grid consists of tiles of 1 unit width and length, you should implement a function that is called from Update() and which constantly checks whether you find yourself one another tile than the previous one. You round every time the X and Y coordinates values, so that you would know on which tile you are at a specific moment. If your function detects that you arrived on another tile, check whether the new location is hole. You should have a List() of your holes location and you iterate through it. Or the better thing would be to actually to label the hole-tiles and instead of running through a whole list, you simply make a check on that new tile.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .