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?
3 Answers
3Have you found an answer? I’m also looking for this
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.
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!
"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." from here: https://forum.unity.com/threads/raycast-on-child-gives-parent-name-in-hit-transform-name.57172/#post-3229888
– raphadko