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

trigger only on collision with player efficiently?

Discussion in 'Scripting' started by steveh2112, Jan 16, 2020.

  1. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    i have an object laying on the ground and it has a tigger so its constantly getting collisions triggers from the terrain collider

    so i have this code which works fine
    Code (CSharp):
    1. public class ArrowPickupTrigger : MonoBehaviour
    2. {
    3.  
    4.     private void OnTriggerEnter(Collider other)
    5.     {
    6.         if (!other.gameObject.CompareTag("Player"))
    7.             return;
    8.  
    9.         GameObject UI = GameObject.Find("UI");
    10.         GameObject pickUpText = Helpers.FindObject(UI, "PickUpText");
    11.         pickUpText.SetActive(true);
    12.  
    13.        // Debug.Log("ArrowPickupTrigger OnTriggerEnter");
    14.     }
    my worry is that its inefficient. i have a feeling that layers are designed to make this more efficient but don't know how. can someone please advise? thanks
     
  2. Emolk

    Emolk

    Joined:
    Feb 11, 2014
    Posts:
    241
    I don't think it matters as it is minuscule.

    But you could make a child object with the trigger on the child object. Then in layers make the child object only interact with the Player in the Physics matrix.
     
  3. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    for a handful of arrows it probably is miniscule but its good to know anyhow. i'll look into layers anyhow, everything is default at the moment