- Home /
Is it possible to merge 2D colliders?
Hello! I am trying to set up a simple rogue-like game, and i am using a nice asset for random dungeon generation... The problem is that every tile is placed in the scene alone, even when building rooms and corridors, so i have a lot of adjacent tiles with single colliders. So i would like to know: is it possible to merge these colliders in a single one somehow? I don't need to have the whole dungeon walls linked with a single collider, just reduce the number of them without having one per single tile. If anyone could help me with this issue, i would really appreciate it!
Thanks in advance, Kae
https://docs.unity3d.com/Manual/class-CompositeCollider2D.html : the colliders you want to merge has to be the children of a gameobject with this component (im late lol)
Answer by Eric5h5 · Sep 18, 2015 at 01:53 AM
Yes, you can read all the colliders and create a combined Polygon2D collider from the others. See PolygonCollider2D in the docs, specifically pathCount and SetPath.
Thanks Eric, great input! I am quite new to scripting, so i am asking further info to set up the process. If you could gently tell me if this is the right way-to-do i would really appreciate it: - i get the coordinates of vertex of each single tile - i list all the data in an array - i create a new polygon2D collider with all the coordinates stored Could it work? Thanks again for your help!
Answer by lassade · Sep 18, 2015 at 12:04 AM
Pick one!
You cannot merge colliders. But you can use a mesh collider and them add all the dungeon walls on the mesh (procedurally generated). (Quite hard)
Create script to SendMessageUpwards just to resend OnCollision and OnTrigger events, put your script that need this calls on a GameObject and make sure that all walls is a parent of this same GO.
Do the same as described in 2 but using static functions, instead of SendMessageUpwards, to avoid the need of all walls been parent of the same GO.
You wouldn't use mesh colliders for 2D; mesh colliders are 3D.
Yeah i overlooked the title and text do not mention about 2d or 3d so i assume the game was 3d. :)