- Home /
OnCollisionEnter of Projectile detects parent with rigidbody instead of child objects with collider
Hey everyone,
I've been searching for an answer for this the past few days and still havent solved it.
THE PROBLEM I have a player controller with an armature who has many child objects for bones. The main script for movement is on the root parent object who is controlled by rigidbody BUT doesnt actually have the collider for the complex shape of my character mesh. It simply has a thin plane collider at the feet to prevent sinking into the ground etc. The child objects of the armature however have various colliders that create a compound collider for detecting collisions on the object in general.
Now, I have a bow and arrow where when you get struck by an arrow the OnCollisionEnter of the arrows script takes the collided object and parents the arrow to it to give it the feel of having stuck on to that object. This works great for my enemy AI who have the same structure as my player but have a NavAgent instead of Rigidbody at the root. For my player, all arrows detect the collision of the PARENT OBJECT and cannot detect the child they actually collided with. I've tried adding a kinematic rigibody to each child with collider which solves the issue but brings other problems and Id rather find a solution where I dont need so many extra rigidbodies. Ive also tried setting up a layer ignore collision matrix but the arrows still detect the player as Im assuming the child objects send the collision message upwards regardless.
Any help will be MUCH appreciated! :)
UPDATE: SOLVED
So if anyone wants to see how I solved this in the future when they experience a similar problem: Basically in my projectile script the OnCollisionEnter's collision parameter would be referring to the parent rigidbody object, however if you write collision.GetContact(0) you will get a reference to the exact contact point and the actual colliders involved. So just do collision.GetContact(0).otherCollider and bam that's your reference to the actual CHILD collider without a rigidbody.
In fact, the awesome and practical thing about using this GetContact method is that I did not change any of my code's structure. Because even for the enemy characters who didnt have a rigidbody and were fine with just the original collision reference, in such a case the collision IS the otherCollider in the GetContact(0) contact. So it's just reference to the same thing.
Hope that clears it up!
Answer by sarpsay · Aug 16, 2019 at 09:21 PM
UPDATE: SOLVED So if anyone wants to see how I solved this in the future when they experience a similar problem: Basically in my projectile script the OnCollisionEnter's collision parameter would be referring to the parent rigidbody object, however if you write collision.GetContact(0) you will get a reference to the exact contact point and the actual colliders involved. So just do collision.GetContact(0).otherCollider and bam that's your reference to the actual CHILD collider without a rigidbody. In fact, the awesome and practical thing about using this GetContact method is that I did not change any of my code's structure. Because even for the enemy characters who didnt have a rigidbody and were fine with just the original collision reference, in such a case the collision IS the otherCollider in the GetContact(0) contact. So it's just reference to the same thing. Hope that clears it up!
Answer by Vega4Life · Aug 16, 2019 at 05:22 PM
This is by design. All colliders that are parented under a rigidbody... are essentially controlled via that rigidbody. As a matter of fact, all the colliders under a rigidbody are formed into on big collider. The only scripts that will get an OnCollision event is the ones sitting on the object with the rigidbody.
If you need to know what collider is being hit. Just have a script that has OnCollision on the rigidBody object. Then when their is a collision just check to see what object got hit.. via a tag, or whatever else. I don't think you would need to have individual scripts on each collider.