How to detect an object in a trigger?

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

    Your logic is totally wrong. You're only checking if the TRIGGER STAYS IN YOUR BOUNDS but still trying to log "Map OFF" message which will never happen.

    Instead of OnTriggerStar method use OnTriggerEnter and OnTriggerExit. Then print the message only when needed ( or in debug mode ) :

    void OnTriggerEnter(Collider other)
    {
        if ( other.gameObject.CompareTag("Player") )
        {
            m_IsPlayerOnTheMap = true;
        } 
    }
    
    void OnTriggerExit(Collider other)
    {
        if( other.gameObject.CompareTag("Player") )
        {
            m_IsPlayerOnTheMap = false;
        }
    }
    
    void Update()
    {
    #if DEBUG
        if ( m_IsPlayerOnTheMap )
        {
            Debug.Log("Map ON");
        }
        else
        {
            Debug.Log("Map OFF");
        }
    #endif
    }
    
    private bool m_IsPlayerOnTheMap = false;
    

提交回复
热议问题