[英]When i used OnTriggerEnter2D with Input.GetKey the input won't work
[英]Input.GetKey("f") does not do anything when f i pressed
在我的范圍內,如果 function, Input.GetKey("t")
命令不起作用。 當我從 if function 中刪除內容並將它們放在外面時,將調用Restart
方法。
using UnityEngine; using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
bool gameHasEnded = false;
public float restartDelay = 2f;
public GameObject LevelFailedUI;
public GameObject LevelCompleteUI;
public void CompleteLevel (){
if (LevelFailedUI == true){
LevelCompleteUI.SetActive(true);
}
}
public void EndGame () {
if (gameHasEnded == false)
{
gameHasEnded = true;
LevelFailedUI.SetActive(true);
if (Input.GetKey("t")) //nothing happens when "t" is pressed.
{
Invoke("Restart", restartDelay);
}
}
}
void Restart ()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
LevelFailedUI.SetActive(false);
} }
它不起作用,因為統一必須在 function 內部,並且在到達if
語句之前還沒有達到檢查,並且您在一幀中按下按鈕,這基本上是不可能的。 您必須不斷檢查輸入,這就是為什么它在Update
function 中完成。
void Update() {
if (Input.GetKey(KeyCode.T) && gameHasEnded) {
Invoke("Restart", restartDelay);
}
}
public void EndGame() {
if (gameHasEnded == false) {
gameHasEnded = true;
LevelFailedUI.SetActive(true);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.