- Home /
How can you change a colliders shape on collision with another collider?
Hello,
I am developing a 2D game where I want to change the shape of an objects 2D collider (collider1) when another collider(collider2) collides with it. The new shape of collider1 will be equal to its original shape without the intersecting section between the two colliders. I hope this makes sense, I've been searching but haven't found or thought of a viable option thus far. Any ideas on how I could go about doing this would be greatly appreciated.
Thanks.
Unity doesn't have APIs for such a complex and advanced collider shapes. Some really big and difficult workarounds are needed to do something like this.
Answer by lgarczyn · Nov 30, 2019 at 07:06 PM
Basically you want your destructible environment to be a polygon collider.
You then want to calculate every point of the polygon which are inside the destructor collider, and name them A.
You the want to transform your destructor collider into a polygon collider, or at least calculate a set of points to define it. Then you want to get all of these points, and select those inside the destructible collider, let's name them B.
You then want to calculate the precise points of intersection between the two shapes, let's name them C.
You then want to remove all of the points A, and add the points B and C. You need to mind the order, and check for all possible weird stuff, like if you just cut the main collider in two (which happens if A contains non-contiguous points).
Basically it's a mess. Interesting exercise, but a hard one.
What you're doing is called a boolean operation (in this case substraction) between two colliders.
This ask mentions the clipper library for this purpose. There may be packages available for that too.
Your answer
Follow this Question
Related Questions
Problem with method Collider2D.isTouchingLayers() 4 Answers
How do I stop characters from standing on top of each other 1 Answer
Player Hit Physics implementation using 2D Animation Frames 0 Answers
Setting a Prefab Clone as the Child of another Object on Collision (2D) 1 Answer
How do I set an object's velocity to the velocity of an object that collided with it? 0 Answers