How to detect an object in a trigger?

后端 未结 3 1696
[愿得一人]
[愿得一人] 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:18

    Use OnTriggerEnter and OnTriggerExit instead of OnTriggerStay to keep the current state:

    public class MapDetect : MonoBehaviour {
    
        private bool isTriggered;
    
        void OnTriggerEnter(Collider other)
        {
            if (other.gameObject.CompareTag("Player"))
                isTriggered = true;
        }
    
        void OnTriggerExit(Collider other)
        {
            if (other.gameObject.CompareTag("Player"))
                isTriggered = false;
        }
    
        void Update(){
            if(Input.GetKey(KeyCode.Space)){
                Debug.Log(isTriggered);
            }
        }
    }
    

提交回复
热议问题