unity脚本中防止多次触发OnTriggerEnter

主要思路就是禁用当前的collider

void OnTriggerEnter2D(Collider2D col)
    {
        if (col.tag == "cherry")
        {
            col.gameObject.GetComponent<BoxCollider2D>().enabled = false;
            Destroy(col.gameObject);
            collections = collections + 1;
            cherText.text = collections.ToString();
            Debug.Log("Triggered by Cherry");
                
        }
    }

 

posted @ 2021-09-14 21:57  gao1996  阅读(1853)  评论(0)    收藏  举报