Unity3d UNITY-如何使UNITY等待我按i键

Unity3d UNITY-如何使UNITY等待我按i键,unity3d,Unity3d,我正在做我在团结的第一场比赛。在这场比赛中,我的球员是一个碰到一些问号的球。当触发问号时,它必须显示一个问题和答案。直到这里一切都好。现在我需要根据问题按a或b。如果答案是正确的,就会增加分数。但问题是。Unity不会等我按键。Unity在我按下按钮和游戏崩溃之前通过了代码 void OnTriggerEnter(Collider collider) { if (collider.gameObject.CompareTag("QuestionCube1")) {

我正在做我在团结的第一场比赛。在这场比赛中,我的球员是一个碰到一些问号的球。当触发问号时,它必须显示一个问题和答案。直到这里一切都好。现在我需要根据问题按a或b。如果答案是正确的,就会增加分数。但问题是。Unity不会等我按键。Unity在我按下按钮和游戏崩溃之前通过了代码

void OnTriggerEnter(Collider collider)
{
    if (collider.gameObject.CompareTag("QuestionCube1"))
    {
        Question.text = "Which Number is bigger?";
        Answer.text = "A.5 B.10";
        if (Input.GetKeyDown(KeyCode.A))
        {
            gameController.minusQuestion‌​Score();
        }
        else if (Input.GeyKeyDown(KeyCode.B))
        {
            gameController.addQuestionSc‌​ore();
        }
        Question.text = "";
        Answer.text = "";
    }
} 
//Sorry if the code is kinda all over the place I dont know how to pass the code here exactly. The gameController and the UI texts are declared and working

好的,让我们离开评论部分,根据我目前对你问题的理解,试着即兴发挥

你首先要考虑的是以下内容:

void OnTriggerEnter(Collider collider)
当另一个碰撞器进入时,仅触发一次。解决方案是什么? 使用OnTiggerStay

void OnTriggerStay(Collider collider)
当对象发生碰撞时,这将始终检查输入

接下来要考虑的是文本的重置。据我所知,当它们不再碰撞时,你应该移除它,这样你就可以有额外的方法。OnTiggerExit,当它们不再冲突时,它将运行额外的代码

void OnTriggerExit(Collider collider)
{
    if (collider.gameObject.CompareTag("QuestionCube1"))
    {
        Question.text = "";
        Answer.text="";
    }
}
所以总的来说

void OnTriggerEnter(Collider collider) 
{ 
    if (collider.gameObject.CompareTag("QuestionCube1")) 
    { 
        Question.text = "Which Number is bigger?"; 
        Answer.text = "A.5 B.10"; 

        if(Input.GetKeyDown(KeyCode.A))
        {
            gameController.minusQuestion‌​Score();
        }
        else if(Input.GeyKeyDown(KeyCode.B))
        {
            gameController.addQuestionSc‌​ore();
        } 
    }
}

您可以使用协同程序在触发输入后等待输入

void OnTriggerEnter(Collider collider)
{

    if (collider.gameObject.CompareTag("QuestionCube1"))
    {
        Question.text = "Which Number is bigger?";
        Answer.text = "A.5 B.10";
        StartCoroutine(WaitForAnswer());
    }
}

IEnumerator WaitForAnswer()
{
    for (;;)
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            gameController.minusQuestion‌​Score();
            break;
        }
        else if (Input.GetKeyDown(KeyCode.B))
        {
            gameController.addQuestionSc‌​ore();
            break;
        }
        yield return null;
    }

    Question.text = "";
    Answer.text = "";
    yield return null;
}

确保你发布的问题代码最少,这样问题就更容易理解和识别。你可以在发布代码时编辑原始帖子,在评论部分,它变得不可读。尽管如此,输入后重置Question.text和Answer.text的原因是什么?要回答您最初的问题,等待输入根本不是一个好的做法。首先,可能没有必要重置文本,或者有非常具体的原因这样做?重置它们的原因是在移动时没有文本出现在我的脸上。好吧,如果这个想法不好,你能给我一个例子,这正是我想要的吗?通过键盘回答问题。我必须在星期六之前完成这项工作。谢谢你的帮助。