Search Unity

Check if child object of my player collides with something.

Discussion in 'Scripting' started by stasgar, Sep 28, 2016.

  1. stasgar

    stasgar

    Joined:
    Sep 6, 2016
    Posts:
    14
    Hi. My object hierarchy is:

    -Player
    --Object

    So the 'Object' is child of 'Player'.

    How to check if 'Object' hits/collide with something?
     
  2. timoshaddix

    timoshaddix

    Joined:
    Jan 7, 2014
    Posts:
    64
    Create a script on the child object,
    and make a function:

    void OnCollisionEnter(Collision other){
    ** Insert your code here for what needs to happen when it collides **
    }
     
  3. stasgar

    stasgar

    Joined:
    Sep 6, 2016
    Posts:
    14
    It's not working, becausse my object is child.
     
  4. ptgodz

    ptgodz

    Joined:
    Aug 4, 2016
    Posts:
    106
    You'll need a collider on the child object to detect collisions, and then use the function that timoshaddix has suggested in a script attached to the child object itself
     
  5. Kalladystine

    Kalladystine

    Joined:
    Jan 12, 2015
    Posts:
    227
    Or you could use events.
    Here is a simple 2-part tutorial for it (not mine).
    In your structure, Player would subscribe to an event in Object. That event should be triggered in the OnCollisionEnter of the child Object (just remember to unsubscribe from the event to not cause "memory leaks").
     
  6. stasgar

    stasgar

    Joined:
    Sep 6, 2016
    Posts:
    14
    I know how to use this function. I'm not a begginer in scripppting. But this function works only in world space, not when an object is a child of something.