按下键时的 C# 和 Unity3D

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11368686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:36:57  来源:igfitidea点击:

C# and Unity3D while key is being pressed

c#unity3d

提问by imperium2335

I am very new to C#. I am creating something in Unity to help me learn C# and Unity better.

我对 C# 很陌生。我正在 Unity 中创建一些东西来帮助我更好地学习 C# 和 Unity。

I want to know why:

我想知道为什么:

Input.GetKeyDown(KeyCode.UpArrow))

Only fires once when placed within:

放置在以下位置时仅触发一次:

void Update()

Since update is a loop, why is it not fired while I hold the key down (in my case causing a sphere to move)?

由于更新是一个循环,为什么当我按住键时它没有被触发(在我的情况下导致球体移动)?

I have managed to get it working by using two bools that are altered when the key is pressed and released.

我设法通过使用按下和释放键时更改的两个布尔值来使其工作。

Here is my full script that I am playing with to move a sphere and simulate acceleration/deceleration:

这是我用来移动球体并模拟加速/减速的完整脚本:

using UnityEngine;
using System.Collections;

public class sphereDriver : MonoBehaviour {
int x ;
bool upPressed = false ;
bool downPressed = false ;
void Start()
{
    x = 0 ;
}

void Update ()
{
    if(x > 0) {
        x -= 1 ;
    }
    if(x < 0) {
        x += 1 ;
    }
    if(Input.GetKeyDown(KeyCode.UpArrow))
    {
        upPressed = true ;
    }
    else if(Input.GetKeyUp(KeyCode.UpArrow))
    {
        upPressed = false ;
    }

    if(upPressed == true)
    {
        x += 5  ;
    }

    if(Input.GetKeyDown(KeyCode.DownArrow))
    {
        downPressed = true ;
    }
    else if(Input.GetKeyUp(KeyCode.DownArrow))
    {
        downPressed = false ;
    }

    if(downPressed == true)
    {
        x -= 5  ;
    }

    transform.Translate(x * Time.deltaTime/10, 0, 0) ;
}

}

采纳答案by redtuna

The documentationsays that this is the normal, expected behavior.

文件说,这是正常的,预期的行为。

You probably want to use GetButtonor GetAxisinstead, since those indicate "pressed" as long as the key is held down (GetButton returns true, GetAxis returns 1).

您可能想改用GetButtonGetAxis,因为只要按住键,它们就会指示“按下”(GetButton 返回 true,GetAxis 返回 1)。

回答by Flickayy

From the documentation on Input.GetKeyDown.

来自Input.GetKeyDown的文档。

You need to call this function from the Update function, since the state gets reset each frame. It will not return true until the user has released the key and pressed it again.

您需要从 Update 函数中调用此函数,因为每帧都会重置状态。在用户松开键并再次按下它之前,它不会返回真值。

This method is most useful for opening menus or other one time events. Such as continuing dialog.

此方法对于打开​​菜单或其他一次性事件最有用。比如继续对话。

You can use the methods as mentioned above, however you can use Input.GetKeyto achieve your goal.

您可以使用上述方法,但是您可以使用Input.GetKey来实现您的目标。

Returns true while the user holds down the key identified by name.

当用户按住 name 标识的键时返回 true。