- Home /
How to add child collison to parrent object
First Look at the image below:
I am trying to make a 2D game but I am using 3D collision to keep the player from walking through the "walls." The green box below the hallway is the objects own collision mesh. The two boxes simulating the walls are just cube primitives with their mesh renders turned off and parented to the hallway.
The problem I am running into is that the hallway object has code that tells it how much health it has and how to respond to impacts and such. But when bullets hit the child objects this code is not triggered and the bullets just bounce off because as far as they are concerned they did not hit a valid target.
All I want to do is add those two collision meshes to get that unique shape. If this is not possible I will have to go back and create custom collision models for every hallway which will be a bit of a pain so I am hoping there is just a simple way to combine the collision meshes so the child objects are consider part of the main object and thus when they get hit the parent object runs the relative code.
Thanks in advance for any help.
[Edit]
At this point I have given up and moved on. I just went ahead and made individual models for each hallway type. I then assigned the 3D model the script, and parented the textured flat plane to it. So now the 3D model handles all the physics and code, and the plane just sits there and looks nice.
Still if anyone figures it out post an answer, it will still be quite useful going forward.
It seems you're using a mesh collider and a box collider. Try removing the box collider?
I can do that but I don't see how that would help. The issue is not the collision of the hallway object, that works fine. The problem is that I want to add the collision of the other two objects to the hallway collision so that when they are hit, the hallway registers a hit.
The only other solution i could think of would be to apply`function OnCollisionEnter(collision : Collision)` to the child object.
Answer by Xtro · Aug 08, 2013 at 07:48 PM
In the screenshot, Hallway doesn't have a script for handling the trigger event and it doesn't have a rigidbody (real event receiver) on it. How are you detecting the event on hallway already? If you explain this, we can come to the real question...
Ah, well its sort of backwards from how I described it. The bullet objects are on the highest layer and cast a ray down onto everything below them. When they are over something they have "hit" it. When a bullet hits something it checks if it has a certain script and then calls a "DealDammage()" function on that object. Since the collision objects don't have that script since they are just meant to act as collision for the parent. So when the bullet "hits" them it does not deal damage to the parent as it should.
Hmmm, if you are casting your own collision detection, you have to search for the parent object of the object you've hit.
Actually, I don't know if this behavior is automatic for composite colliders parented by a rigidbody when using regular collision or trigger events.
But anyway, you are not using regular collision. When you hit something with the raycasting, try to search up to the root until you find a parent script object (The object you'll send the dealdamage command)
searching for a parent script:
// ... your raycast code here
// "Hit" contains the hit info from the raycast above
var Transform = Hit.collider.transform;
// replace your root script with ScriptXXX
ScriptXXX DetectedParent = null;
while (Transform){
var Parent = Transform.parent.GetComponent<ScriptXXX>();
if (Parent){
DetectedParent = Parent;
break;
}
Transform = Transform.parent;
}
if (DetectedParent) DetectedParent.DealDamage;
else DoNothing(); // we couldn't find a suitable parent :)