Unity Input.GetKeyDown(KeyCode.Space)无法检测到按键按下

6
我正在学习Unity,但我的按键输入未被检测到。
    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    public Rigidbody myBody;

    private float time = 0.0f;
    private bool isMoving = false;
    private bool isJumpPressed = false;

    void Start(){
        myBody = GetComponent<Rigidbody>();
    }
    void Update()
    {
        isJumpPressed = Input.GetKeyDown(KeyCode.Space);
        Debug.Log(isJumpPressed);
    }

    void FixedUpdate(){
        if(isJumpPressed){
            myBody.velocity = new Vector3(0,10,0);
            isMoving = true;
            Debug.Log("jump");
        }
        if(isMoving){
            time = time + Time.fixedDeltaTime;
            if(time>10.0f)
            {
                //Debug.Log( Debug.Log(gameObject.transform.position.y + " : " + time));
                time = 0.0f;
            }
        }
    }

}  

为什么isJumpPressed总是false? 我做错了什么? 从我的理解来看,这应该是有效的,但显然我漏掉了一些东西。 更新: 感谢所有提出想法的人。 当我停止尝试检测空格键时,我成功使isJumpPressed返回true。
isJumpPressed = Input.GetKeyDown("a");

有人知道为什么这个会起作用而另一个却不行吗?

isJumpPressed = Input.GetKeyDown(KeyCode.Space);

或者
isJumpPressed = Input.GetKeyDown("space");

更新2:显然,这是Linux的一个bug。我已经读到说,当游戏只在编辑器中构建时,不会发生这种情况。我在https://forum.unity.com/threads/space-not-working.946974/?_ga=2.25366461.1247665695.1598713842-86850982.1598713842#post-6188199找到了一个解决方法。

如果有谷歌员工遇到这个问题,请参考以下代码,因为这对我有效。

public class Player : MonoBehaviour
{

    public Rigidbody myBody;

    private float time = 0.0f;
    private bool isMoving = false;
    private bool isJumpPressed = false;

    void Start(){
        myBody = GetComponent<Rigidbody>();
    }
    void Update()
    {
        isJumpPressed = Input.GetKeyDown(SpacebarKey());
        if(isJumpPressed)
        {
            Debug.Log(isJumpPressed);
        }
    }

    void FixedUpdate(){
        if(isJumpPressed){
            myBody.velocity = new Vector3(0,10,0);
            isMoving = true;
            Debug.Log("jump");
        }
        if(isMoving){
            time = time + Time.fixedDeltaTime;
            if(time>10.0f)
            {
                //Debug.Log( Debug.Log(gameObject.transform.position.y + " : " + time));
                time = 0.0f;
            }
        }
    }

    public static KeyCode SpacebarKey() {
        if (Application.isEditor) return KeyCode.O;
        else return KeyCode.Space;
    }

} 

这是一个奇怪的情况,你的代码应该可以工作。我查看并找到了一些信息,建议你尝试禁用并重新启用游戏对象。 - ChilliPenguin
大家好,感谢你们的评论。当我使用isJumpPressed = Input.GetKeyDown("a")时,代码可以正常工作。但我完全不明白为什么它无法检测到我的空格键。有人有什么想法吗? - jakegergen
3个回答

4
问题在于FixedUpdateUpdate不会依次调用。Update在每帧只被调用一次,而FixedUpdate在每个物理更新时被调用(默认为每秒50次更新)。
所以以下情况可能发生:
Update is called -> GetKeyDown is true (this frame only) ->  isJumpPressed = true
Update is called -> GetKeyDown is false ->  isJumpPressed = false
FixedUpdate is called -> isJumpPressed is false 

这是一个示例,每次按空格键时都会打印“Update Jump”,有时会打印“FixedUpdate Jump”。不要这样做:
bool isJumpPressed;
void Update()
{
    isJumpPressed = Input.GetKeyDown(KeyCode.Space);
    if (isJumpPressed)
    {            
        Debug.Log("Update Jump");
    }       
}

private void FixedUpdate()
{
    if (isJumpPressed)
    {
        Debug.Log("FixedUpdate Jump");            
    }
}

请采用以下方法:

bool isJumpPressed;
void Update()
{        
    if (Input.GetKeyDown(KeyCode.Space))
    {
        isJumpPressed = true;
        Debug.Log("Update Jump");
    }        
}

private void FixedUpdate()
{
    if (isJumpPressed)
    {
        Debug.Log("FixedUpdate Jump");
        isJumpPressed = false;
    }
}

这对我没用。控制台中没有输出任何内容。 - jakegergen
@jakegergen 以防万一:请确保脚本已附加到游戏对象并启用,您处于播放模式且游戏窗口处于焦点状态。 - Pluto
感谢您的评论。脚本已附加到游戏对象并启用。 - jakegergen
@jakegergen 即使通过修复空格键,你仍然会遇到我在答案中提到的问题,所以你应该修改你的代码。 - Pluto

2
可能的问题之一是您的项目可能正在使用新的输入系统包。如果您正在使用此软件包,则旧的输入管理器功能将无法正常工作。要检查这一点,请转到编辑>项目设置...>播放器>其他设置,并且活动输入处理应设置为输入管理器(旧)(或者两者都可以)。

如果您确实想使用输入系统包,则必须安装它(如果尚未安装),并且您可以按以下方式检查是否按下了空格键:

using UnityEngine.InputSystem;

...

jumpPressed = Keyboard.current.space.wasPressedThisFrame;

谢谢您的建议,我没有考虑到这一点。然而,我的输入处理正在使用旧的输入管理器。 - jakegergen

1

我也遇到了同样的问题。花费数小时尝试找到解决方法。 可能从一开始就没什么问题,直到我发现。 当我执行播放以查看结果和测试移动时,错误地选择了场景选项卡。如果我想要查看结果(播放),我必须点击“游戏选项卡”。在游戏选项卡中,键被检测到。 在此输入图片描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接