Distinguishing multiple colliders on one object
I'm working on a simple helicopter simulation. I want the helicopter to have different colliders for the rotor and the landing gear, with different behaviors depending on which of the two is having a collision.
Several other answers to similar questions I found suggest adding empty child objects to the parent object adding and the different colliders and scripts to different child object. But when I do that, the OnCollisionStay and similar functions do not trigger in scripts attached to the child objects. Only the functions in the script of the parent object triggers if any of the child colliders hit something. Whether or not the parent object has a collider of its own doesn't seem to matter.
I wouldn't actually mind that all the trigger signals are received by the parent object, as that's where I control the rotation and speed of the helicopter. But I still can't distinguish by which collider triggered the collision event.
Does anyone know either a way for me to distinguish the collision triggers inside the parent object, or to have the collision events fire in the child events that contain the script and collider instead of the parent object?
PS: I haven't set the colliders to IsTrigger=true, because I appreciate not having to make sure myself that a landed helicopter doesn't fall through whatever it landed on. If neccesary I could change that.
Answer by bificommander · May 06, 2017 at 07:21 AM
I found this solution
void OnCollisionEnter(Collision collision)
{
foreach(ContactPoint contact in collision.contacts)
{
var colName = contact.thisCollider.name;
switch (colName)
{
case "Rotor":
//Do something
break;
case "LandingGear":
//Do something
break;
case "Body":
//Do something
break;
}
}
}
I suppose each collider would have to be placed on a separate child gameobject so that you can get the name of the object the collider is on.
Answer by Szarley-Dwarf · Apr 30, 2017 at 05:10 PM
I found that kind of solution works on some occasion for a colliders on one object.
if (other.GetType () == typeof(EdgeCollider2D)) {
//do stuff
} else if(other.GetType() == typeof(CircleCollider2D)){
// do stuff
}
it didn't work for me :/ but maybe that will be helpful for you.
Thanks. I'll give it a shot tomorrow. Question though: Since this code snippet refers to "other", is this code supposed to go into the objects that the helicopter can collide with (i.e. the ground and all buildings)?
I'm not sure to be honest. I'm Game Dev & Unity newbie and just start to learn it. I place that code on the player object on which I check the collisions with other world objects. If that was wrong let me know.
Your answer
Follow this Question
Related Questions
Enemy Trigger Colliders are triggering my player's trigger collider. Why? 1 Answer
Player respawn after collision 0 Answers
Untouchable child objects of first person controller 1 Answer
Find a collision point from inside the object. 0 Answers
URGENT!!! Colliders don't respond/move with imported Maya animation? 3 Answers