Creating gameobjects with multiple colliders [image heavy]

(reposted from the Scripting forum after I realized it was more suited to here)

I’m having some really confusing issues with creating multiple child box colliders for a helicopter game I’m working on. Rather than use a mesh collider, I decided I wanted to box off regions of the helicopter’s model so that I could have more complex damage detection.

I created my helicopter gameobject, and then added a rigidbody to it. Then, following the guide on Unity - Manual: Box collider component reference I created an empty game object for each ‘area’ of the helicopter. Then, I added box colliders to each of those game objects and made the helicopter gameobject with the rigidbody their parent.

At this point, everything seemed to work fine. I placed the copter onto my terrain, I hit play, it settled down into place happily on the colliders I had set up for the landing gear. At this point I figured the next step was that I should place a script into each collider to pass along collisions to the parent Flight Control script, so it could handle calculating damage. I placed the following script into each collider:

function OnCollisionEnter(collision : Collision)
{
	var arr = new Array();
	arr.push(collision);
	arr.push(gameObject);
    gameObject.SendMessageUpwards("ColliderHit", arr);
}

And in the Flight Control script, I added the following receiver function:

function ColliderHit(data : Array)
{
	var collision : Collision = data[0];
	var source : GameObject = data[1];
	Debug.Log(source.name + " collided with " + collision.gameObject.name);
}

However, when I ran my scene the next time, the collision event didn’t fire. Eventually I came to suspect that this was because my child colliders needed rigidbodies too, and that the parent helicopter object having a rigidbody wasn’t enough. At this point, my objects looked like this:

So, I went ahead and added a rigidbody to the collider I had defined for the front landing gear. I hit play… The nose of the copter went falling through the ground, and the entire collider I had defined bounced off of its parent entirely…

On the upside, the script event did trigger this time.

However, this result made little sense to me, because in the tutorial I linked earlier, it states that child rigidbodies will be dragged along with the parent. I didn’t expect it to just fall off like that. After some further reading I figured my issue was maybe that I had to mark the rigidbody on the collider as kinematic. I went ahead and did so, and this time, even though the collider stayed in place, the entire front of the copter fell through the ground again! The collider would no longer be stopped by the terrain, and it no longer generated any script events for collisions.

What makes even less sense here, is that while in the image above you can very clearly see that the rear landing gear are staying above the terrain, the nose, cockpit and several other colliders seem to have randomly changed their behaviour and are clipping through the ground now as well, even though they dont even have rigidbodies. Adding one rigidbody and changing it to kinematic seems to have affected half the Box Colliders on the entire object!

So, my question is, what am I doing wrong here? As far as I can tell, a normal rigid body can’t move with a parent, and a kinematic rigidbody won’t register collisions against terrain or raise collision events in scripts… How is it possible, then, to make a gameobject with multiple hit areas that can detect collisions and pass them to the parent?

Very cool helicopter !
collider can work without rigidbody, I don’t know about the trigger, but it works well for line check.
I had a car with children colliders and using line check(or ray check), which works well.
I think maybe it’s because the layers of the colliders. They penetrate with each other, could that be the reason why the trigger not work?

And the parent has a rigidbody? I didn’t try this before, but I think the rigidbody of the parent will move according only to the collider of the parent.

I think it would be difficult if you want to simulate the copter hit the ground.I’d rather explode it with a very cool particle effect.

Ive tried moving all the child objects out of the Collders gameobject and into the main heirarchy but it produces the same results. Ive also set all the Layers and the Physics Collision Matrix not to register the colliders against one another, so I am sure they are not colliding with one another.

When there is no rigidbody, the colliders work 100% fine, its just that they also dont raise events in scripts. When I add the rigidbody, they fall off the copter or in the case of kinematic, they simply do not collide with anything or even register collision events.

Making the helicopter just explode really isnt an option unfortunately – This is a simulator, and I need to be able to measure the force of impact against the landing gear during landings. My helicopter flight script works perfectly, as does landing and flying it with the non-rigidbody colliders, its just that I have no way of detecting collisions in my scripts.

Seriously, nobody?

This can’t be an uncommon occurance, LOTS of games use multiple hitboxes on objects!

Either I’m doing something wrong or this is a bug, it -can’t- be this difficult to simply make my colliders detect collisions AND also not go through things/fly off the parent.

A collider by it self is kind of a bastard step child of the physics engine. You need to add rigid bodies to each collider to receive all the nice event messages (this is actually documented, if you bothered to read, that is). But then you run into the issue of bits of your copter falling apart. Solution? Fix all the rigid bodies together using Fixed Joints to the main rigid body. Profit!

I made it as far as the rigid bodies (and did read the documentation) but had no idea joints even really existed, that was the missing puzzle piece, THANK YOU!

Well, I thought I had this solved, but no such luck yet. Fixed joints don’t appear to do the trick – They wobble around and bounce crazily, making it impossible to use the helicopter. Here’s a picure of what happened to the Front Landing Gear collider/rigidbody when I tried to land after adding a Fixed Joint

I’ve now tried with Hinge Joints instead, with limits set to 0 so theoretically they can’t move… The collision boxes STILL bounce and spring around like crazy.

This is incredibly disappointing that something so simple and common is so hard to do in Unity.

In the future, please ask a mod to move the topic instead of posting a duplicate.

Anyway, it’s a feature that child colliders act as a single collider. I wouldn’t try to make them into separate objects with rigidbodies; you can use the data from Collision.contacts to see what area was hit.

–Eric

I’d rather not add rigidbodies to them at all (the collision and positions of them all worked fine before I did), but it seems like the child colliders don’t raise scripted collision events unless each and every one of them has a rigidbody of its own.

Is there no way to recieve child collider events on the parent rigidbody?

Yes, as Eric said in the previous post - use Collision.contacts.

So you should be able to have your chopper set up with compound colliders as you did in your first post where everything worked to your satisfaction. You then have a single script on your parent collider which handles collisions. Any collision on a child collider should raise the event on your parent script. Then use the contacts to get which collider caused the event.

See here: Unity - Scripting API: ContactPoint
Use “thiscollider” to get which of your sub-colliders is actually colliding.

1 Like

for characters in my scene I have a health script and rigidbody on the parent with the colliders in their children game objects. On my bullet script i send a damage message to what it collides with if it has a certain tag. this works on one of my characters, it receives damage when the bullet hits one of its collisions. but not on the other, using the same health script and everything nothing happens, they are almost identical in the components and colliders on them!

Anyone know why this could be?