- Home /
Getting a collision's bottom-most collider in a hierarchy
Hi!
I've got a projectile colliding with a planet to which various buildings are attached as childs. All have colliders. I'd like to know when a building is hit, but OnCollisionEnter's Collision.collider only gives the topmost GameObject, the planet.
I just read that I could look at the contacts list, but is this really the only way? Isn't there a way to get "collider" to just give the bottommost object directly?
I also know about the raycasting option, but that sounds like an irritating and costly workaround to me.
Thanks!
Answer by Joshua · Apr 25, 2011 at 04:42 AM
Actually, I think raycasting is the easiest way. It seems complicated when you've never used it, but read up on it here and try it. :)
You know, you're right, it doesn't look that complicated. I'm only annoyed at all the little things that Unity's physics got wrong. Thanks, I'll look into it. I'll still wait a bit for other potential answers to show up though.
I used to feel exactly the same. But now that I feel I understand more of it it's rather that the physics is enormously powerful, but has to be used correctly. And it's designed for non-stressed situations because it's made to run in the background of every project - so it's not perfect in every situations, it's made to always be 'good' and not recourse intensive.
So, with the raycasting method, you don't need a collider on the source object right? Same with spherecasts and the like?
You do. That's what the ray intersects with and stores in it's raycastHit variable.
Answer by Casper 1 · Apr 25, 2011 at 10:26 AM
You can use GetComponentsInChildren to find the nested colliders:
Collider[] childrenColliders = other.GetComponentsInChildren<Collider>();
(Assuming other is your planet collider)
Sure, but that doesn't tell me exactly which children's collider actually got hit, does it?
I'm just guessing here, but couldn't you cross reference this with the contacts list and then take the entry of your childrenColliders array with the highest index? This will be the bottommost collider since they're added by parent/child and alhabetical order.
Well in the case of my projectile, it hits only one target at any one time, so the contacts list always contains the exact object that got hit. That works. But Collision.collider is still utterly useless.
Your answer
Follow this Question
Related Questions
Weird Collision Detection with a Cube and OnCollisionEnter 2 Answers
Collider isn't working ? 2 Answers
Detect collision point?? 1 Answer
OnCollisionEnter of Projectile detects parent with rigidbody instead of child objects with collider 2 Answers
Check if collider is colliding with anything non specific? 1 Answer