我是 Unity 游戏开发的新手,并从一些教程开始。我面临的问题是来自键盘的击键输入无法被统一识别/拾取。我在网上查了一些解决方案,但更困惑请求帮助。
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) == true)
{
Debug.Log("Space key pressed");
myRigidbody.linearVelocity = Vector2.up * FlapStrength;
}
else
Debug.Log("Key Stroke Not Received");
}
调试:
Key Stroke Not Received
UnityEngine.Debug:Log (object)
Flappy_Bird:Update () (at Assets/Flappy_Bird.cs:22)
Key Stroke Not Received
UnityEngine.Debug:Log (object)
Flappy_Bird:Update () (at Assets/Flappy_Bird.cs:22)
Key Stroke Not Received
UnityEngine.Debug:Log (object)
Flappy_Bird:Update () (at Assets/Flappy_Bird.cs:22)
Key Stroke Not Received
UnityEngine.Debug:Log (object)
Flappy_Bird:Update () (at Assets/Flappy_Bird.cs:22)
我尝试检查输入管理器中的设置并重新安装 Unity。在这里检查了解决方案,但没有找到可以解决此问题的解决方案。
确保游戏窗口在运行时具有焦点。
单击检查器或编辑器中的其他位置将阻止游戏窗口捕获输入。
按“开始”后单击游戏窗口内部以确保其具有焦点。
它也将有助于您彻底删除
else
语句,以免控制台充斥着无意义的日志。 在数千条消息中找到一条消息几乎是不可能的。 else 语句日志无论如何都不会告诉您任何信息,只有 if 语句日志相关。
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log($"Space Pressed!");
}
}