- Home /
Combining two hit box (colliders) to become one
Looking for the best way to combine two hit boxes into one (on collision) creating link between the two objects.
Any Suggestions? Thanks - C
do you want to combine two meshes or coliders? just a movement/rotation link? (parent one of them to the other if yes.)
Please accept an answer, or mark answers as not helpful.
Answer by Firedan1176 · Jun 06, 2015 at 01:24 AM
Once the two collide, you can use Physics.IgnoreCollision(object1.Collider, object2.Collider)
, which will prevent them from colliding. Once the area in either is greater than ~70%, you could destroy one of them, then lerp the size of the new one to be as big as both combined.
Answer by Statement · Mar 15, 2011 at 03:33 PM
If your objects both have rigidbodies, then I guess you'd need to remove one of them upon collision. Next you should parent the object which rigidbody was removed to the one with the rigidbody still on. Note that the resulting combined collider will remain the same shape of the two separate ones, it won't build a "big collider" as in your drawing with your circles.
Some naive code would look like this but this won't work since you'd get the same callback on both objects. You need to take into consideration the rules for which of the rigid bodies that should be the parent. It only serves as a guide how you could go on with the actual parenting:
// Warning, this code won't work without extra checks!
void OnCollisionEnter(Collision collision)
{
Destroy(collision.rigidbody);
collision.transform.parent = transform;
}
Answer by efge · Mar 15, 2011 at 03:49 PM
Another way to get one new object could be something like this:
Create a new empty GameObject in the center of the hit boxes.
Attach a Rigidbody and Collider to the new object.
Use Collider.bounds to calculate the size for the new Collider.
Use Rigidbody.mass to calculate the mass for the new Rigidbody.
Remove the Rigidbodies and Colliders to the old objects.
(You could also compare the masses of the old objects to adjust Rigidbody.centerOfMass for the new Rigidbody.)
Your answer
Follow this Question
Related Questions
creating active objects or spots otherwise said HotSpots 1 Answer
How to combine sprites and make 1 collider for them 1 Answer
Is it possible to detect which side of a sprite a touch came in from? 0 Answers
My Raycast Won't Fire 1 Answer
Fastest way to add calculate damage on different body areas (hit boxes) 1 Answer