Search code examples
unity-game-engine2dspritephysicscollider

Is there a way to recalculate the polygon collider at runtime? and possibly with better accuracy?


so I have this game in which I have destructible terrain. my setup is that when terrain is destroyed a part of the terrain texture is made transparent, to act as a crater. after any kind of change takes place, I delete the polygon collider on the texture for the terrain and create a new one in order to recalculate the terrain, but obviously this is very taxing on processing power. is there a way to reset the polygon collider without having to do this?

in addition, the polygon collider is not very accurate and I've been told that pixel perfect collision is not possible in unity, but I hold out hope. is there a way to increase the accuracy at least?

here is an example of my problem:

enter image description here


Solution

  • Collider recalculation is expensive, and there's no way around it in Unity, you can't even do it asynchronously.

    It depends on your exact situation, but for a Worms-type game with dynamic terrain destruction, I wouldn't use polygonal colliders/Unity physics engine at all. I'd create my own pixel-perfect collision system that would rely only on the underlying terrain texture. You already have the terrain data, checking for collisions could be done with Bresenham's algorithm.

    Here's a tutorial: http://gamedevelopment.tutsplus.com/tutorials/coding-destructible-pixel-terrain-how-to-make-everything-explode--gamedev-45