unity觸發器碰撞調用OnTriggerStay()檢查按鍵多次執行的問題


OnTriggerStay被隨機調用,永遠不要檢查其內部的Input。

在OnTriggerEnter和OnTriggerExit函數中設置為true和false的標志,然后檢查該標志並輸入Update函數(每幀)。

private void Update()
{
    if (Input.GetKeyDown(KeyCode.E) && triggerStay)
    {
        //
    }
}

bool triggerStay = false;

void OnTriggerEnter2D(Collider2D collision)
{
    Debug.Log("Entered");
    if (collision.gameObject.CompareTag("InteractiveArea"))
    {
        triggerStay = true;
    }
}

void OnTriggerExit2D(Collider2D collision)
{
    Debug.Log("Exited");
    if (collision.gameObject.CompareTag("InteractiveArea"))
    {
        triggerStay = false;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM