Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Raycasting a child object doesn't work, even if parent has a box collider

Discussion in 'Scripting' started by LambdaTheDev, Jul 2, 2021.

  1. LambdaTheDev

    LambdaTheDev

    Joined:
    Jan 15, 2020
    Posts:
    53
    I am working on the item pickup/drop system. This is how the drop looks:
    ```
    - Networked object is spawned, with item data
    - That networked object spawns the item model, as a child of it
    - Networked object contains small box collider (I have read some questions about child object raycasting)
    - Model contains MeshCollider
    ```
    And I want to be able to raycast child object.
    I can't just do fixed size collider on a parent, because some items are bigger (weapons), and some smaller (keys).

    Does anyone know what to do?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Raycasts don't really care about parent/child relationships. They just look for colliders. What are you trying that's not working?
     
  3. LambdaTheDev

    LambdaTheDev

    Joined:
    Jan 15, 2020
    Posts:
    53
    PARENT OBJECT:
    - NetworkedDroppedItem component (item data)

    CHILD:
    - Specific item model
    - MeshCollider

    I try to raycast the child object, just with Physics.Raycast(ray, out RaycastHit hit, 20f).

    I just want to make RaycastHit a child object.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    That will work assuming your ray is correct. Maybe the object is not where you think it is or you formed your ray incorrectly.