- Home /
How do I use a 2D polygon collider to generate a correct collider for my randomly generated mesh?
Currently I am making a randomly generated 2d tile based game in which a map generator object creates a large number of chunks and then each chunk generates a square of tiles. I have the random generation(or at least part of it) but the idea of collision evades me. The object is a mesh with each tile consisting of 2 triangles but it seems like a polygon collider where I feed it the vertices is the best option since not every tile is its own gameobject and I can't just put box colliders on all of them. My question is how would I use a polygon collider to make the darker regions have collisions while the lighter regions not have collision?
Since polygon colliders have one array of vertices I was wondering how the component determines what the inside of the collider is and how to use a segmented mesh with a polygon collider because at some point my random generation would lead to a chunk being split in two.
Dont' worry about the error, that is just unity freaking out about me editing setting while the game is running
Have you found a solution to your problem? I am currently having a simmilar problem and don't see much in the documentation.
Yeah, I did come to a solution but the answer is on a similar question. https://answers.unity.com/questions/1511762/how-to-make-a-2d-polygon-collider-from-a-mesh.html?childToView=1513286#answer-1513286 should be the link. I included the code as well. The answer involved making an array of all the edges of every tile and removing all edges that overlap using the solid mesh as a starting point thus giving only the edge edges. Then you have to order the edges then finally put the points of the edges into a polygon collider
Good day.
You can make a collider for each tile. If you group all the tiles you need as a child of a parent gameobject, and detect collisions from this parent, all child colliders will act as only 1 collider.
You also have the mesh collider.
You cal aslo have a collider in each tile (so they know what tile type is), and each tile check for collision with objects/player, etc..
Your answer
Follow this Question
Related Questions
Efficient generation of 2d polygon collider 0 Answers
Weapon System with collide detection (Helps with script pls)!!! 0 Answers
Physics2D, Box Collider physics inaccuracy? 1 Answer
How to use Physics2D.IgnoreCollision2D in Unity 5 to cancel a trigger 0 Answers
2D Collision not working! (child sprites colliders) 1 Answer