簡體   English   中英

如何檢查 OnTriggerEnter function 在 Unity 的“if”條件下是否被調用?

[英]How do I check whether the OnTriggerEnter function is called or not in an “if” condition in Unity?

我正在開發一個 3D-Bird 游戲,如果它通過管道(其上附有一個盒式對撞機及其遠端),則得分或不得分。

如果鳥得一分,我可以更新分數。 但如果這只鳥沒有達到它應該得分的分數,我應該能夠結束比賽。

為此,在控制腳本(附加到一個空對象)中,我正在跟蹤鳥的 Z position 和管道的 Z position(Z 是鳥向前運動的方向)。如果 Z Z4757FE07FD492A8BEEZA6A76鳥大於pipe的Z position,則鳥已通過pipe。 同時我想檢查是否得分,即 OnTriggerEnter function 是否被調用。 我該怎么做呢?

部分代碼如下:

if (GameControl.instance.gameOver == false 
       && CurrentZpositionOfBird>CurrentZpositionOfScoringCollider 
       && ____________) {

在上面的空白處我想檢查 OnTriggerEnter function 是否被調用

OntriggerEnter不是您檢查的bool ,如果輸入了某些內容,則觸發器返回 true(除非您自己編寫代碼)。

它是一個“聽眾”。 它會監聽,因此當觸發器或對撞機進入定義MonobehaviourOntriggerEnter的組件游戲對象層次結構之一時,將被執行。 將其理解為事件偵聽器。 它正在偵聽的事件可以通過方法名稱本身來理解。 通常在使用方法名稱為OnWhatever()的編程中

文檔非常好。 你需要檢查一下。 無論如何評論以下示例:

public class exampleScript : MonoBehaviour //This class is attached to a gameobj in the scene
{


    //This OnTriggerEnter method will be listening, so that when other 
    //trigger or collider  geometrically intersecs with yours,
    //the code inside this method will be executed. For this case just the 
    //log in the console, but can be whatever you might need to be done.

    private void OnTriggerEnter(Collider other)
    {
        Debug.Log("Entered!");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM