How to detect an object in a trigger?

后端 未结 3 1596
[愿得一人]
[愿得一人] 2021-01-20 01:49

I’ve placed in the scene an object with a trigger and I want the console sends me a message detecting if the player is in or out of the trigger when I click a button . When

3条回答
  •  无人共我
    2021-01-20 02:28

    Try:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class MapDetect : MonoBehaviour {
    
    
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag("Player"))
         {
             Debug.Log ("Map ON");
         } 
     }
    
     void OnTriggerExit(Collider other)
     {
         if (other.gameObject.CompareTag("Player"))
         {
             Debug.Log ("Map OFF");
         }
     }
    }
    

    This will switch it on when you enter and off when you exit (althout all it does right now is print the result).

    Hope it helps.

提交回复
热议问题