Search code examples
unity-game-engine2dmeshmemory-efficientcollider

What is the most efficient way to create a group of 2D tiles with 2D colliders in Unity3D?


First of all, I am using Unity3d.

What is the most efficient (in terms of memory) way to create a group of 2D tiles with 2D colliders using a texture atlas (and tile data)?

Background Info: I am working on a 2D terrain generation asset. It is a very similar generation style to Terraria's random generation. Currently, each tile is being instantiated as a separate GameObject. As I now know, this is extremely inefficient, and I should use a texture atlas and tile data instead. Here is a link to a tutorial I have been following that deals with this in 3D: http://studentgamedev.blogspot.co.uk/2013/08/unity-voxel-tutorial-part-1-generating.html

The problem is that mesh colliders are 3D colliders; 3D colliders CANNOT collide with 2D colliders. Currently in Unity, there are no 2D colliders (that I am aware of) that have the properties of a mesh; I need to dynamically change the 2D collider to adjust to the positions that contain tiles. How am I supposed to develop an efficient 2D tile system using 2D colliders?

Here are some of my ideas of techniques that may work:

  • Add a box collider 2D component to the chunk GameObject for each tile in the chunk.
  • Somehow dynamically use a polygon collider 2D to stretch over all solid tiles.

I have read through several threads and cannot find a good approach for this problem. I am mostly looking for a proven technique/approach to this issue, but I am open to any suggestions or techniques. I am happy to provide clarity as needed. Thanks for any answers! I appreciate the time you put in to answering my question -it helps a ton!


Solution

  • As you rightly said, 2D colliders do not work with 3D colliders. The correct approach would be to pick one or the other.

    If you are mixing 2D and 3D objects, go with 3D. You can restrict the axes in which your objects are affected by physics as explained in this answer.