Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Check if Player tag enters a collider on child object?

Discussion in 'Scripting' started by Studio_Akiba, May 3, 2015.

  1. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    For design reasons I have put my trigger collider (capsule collider) on the child object.
    I need to check if the player collides with it, is this possible or do I need to rethink my design?
     
  2. relic1882

    relic1882

    Joined:
    Mar 11, 2015
    Posts:
    45
    I used tags to detect collision on my characters with my player. Tag your main player as "Player" or whatever you want then check it's collision using OnTriggerEnter. I did my player collision checks on a separate child object in my project as well. It's working quite well for me. You can use the same method to grab the tag of the object hitting your trigger with OnTriggerStay and OnTriggerExit as well.

    I made a trigger collider on a child object on my NPC that walks around and when my Player character gets too close, the NPC reacts.

    Code (CSharp):
    1.     void OnTriggerEnter(Collider other)
    2.     {
    3.  
    4.         if (other.gameObject.tag == "Player")
    5.         {
    6.                 //do stuff or react
    7.         }
    8.     }