- Home /
Raycast only returning parent object; need child hit
I need to get the name of the child object hit by a Raycast, but everything I've tried just returns the name of the parent object. Does anyone know the solution to this?
Answer by raphadko · Sep 25, 2017 at 07:30 PM
Have you found an answer? I'm also looking for this
"Colliders on child objects are treated as though they are additions to the parent collider (this is what you want for most physics tasks). However, the RaycastHit object also has a field for the collider that was hit. The reported transform is the parent's transform but the collider is the exact collider that was hit. If you need the transform of the child object, you can use the collider's transform property, etc."
Answer by Leohd · Apr 22, 2021 at 12:30 AM
It's very counter intuitive, hit.transform returns the parent's transform of the hit, yet hit.collider returns the specific collider of the hit.
Answer by DirtyDilara · May 29, 2021 at 09:15 AM
Hey thank you @Leohd, you saved me a lot of time.
And I´ve tested something more out: hit.transform returns the transform of the "nearest" Rigidbody in the Hierarchy. hit.collider.transform returns the transform of the real hitted Collider.
And it can result into a problem, if you have something like a fbx models with multiple objects and you setup the parent of these with a Rigidbody and the childs of these with Convex MeshColliders for example.
Best regards!
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Having trouble with raycast rigidbody object childing for tractor beam 1 Answer
Ignoring the raycast of a child collider but not parent. 2 Answers
How can I differentiate between colliding with an object or its child? 1 Answer
Raycasting from child weird behaviour 2 Answers