0
\$\begingroup\$
  • I have a LineRenderer that ignores certain Layers based on a Raycast using LayerMask.

  • I have a cube with a collider on a Layer which is ignored because of the LayerMask.

  • I have a smaller object inside the cube that is a child object of the cube and is on a layer that is not set to be ignored. I need the Raycast to hit the smaller object.

When this smaller object is un-parented from the larger cube, the LineRenderer behaves like I want it to: the hit data informs me it is being hit by the Raycast, but this does not hold true if the smaller object is a child of the cube.

Is there a way of having this behaviour without having to un-parent the smaller object? It's like the smaller object is inheriting the parent's Layer, despite being on it's own Layer - does this sound like what is happening?

\$\endgroup\$
  • 1
    \$\begingroup\$ It sounds like something else is going on. Parent/child relationships between GameObjects has no bearing on raycasts on its own. The only things that such relationships affect are transformation and active state (plus some utility stuff, like broadcasting messages, finding components, optionally changing layer assignments in the editor after prompting the user, etc.) \$\endgroup\$ – Ed Marty Sep 14 '17 at 12:31
1
\$\begingroup\$

The Raycast was actually hitting the child object but returning data for the parent object. It is possible however to return information about the child by accessing the collider that was hit (that of the child object).

So to access the child collider:

hit.collider.transform

instead of just:

hit.transform.

You can then access the gameObject, transform or whatever from the collider.

From the Unity docs for RaycastHit.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.