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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Check if Player tag enters a collider on child object?

Discussion in 'Scripting' started by ATLAS-INTERACTIVE, May 3, 2015.

  1. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    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.     }