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

jak*_*gen 7 c# unity-game-engine

我正在学习 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;
            }
        }
    }

}  
Run Code Online (Sandbox Code Playgroud)

为什么 isJumpPressed 总是 false。我究竟做错了什么?据我了解,这应该有效,但我显然错过了一些东西

更新:感谢所有提出想法的人。当我停止尝试检测空格键时,isJumpPressed 返回 true。

isJumpPressed = Input.GetKeyDown("a");
Run Code Online (Sandbox Code Playgroud)

任何人都知道为什么这可行而不可行

isJumpPressed = Input.GetKeyDown(KeyCode.Space);
Run Code Online (Sandbox Code Playgroud)

或者

isJumpPressed = Input.GetKeyDown("space");
Run Code Online (Sandbox Code Playgroud)

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

如果任何 Google 员工偶然发现此问题,请参考以下代码,因为这对我有用。

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;
    }

} 
Run Code Online (Sandbox Code Playgroud)

Plu*_*uto 5

问题是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 
Run Code Online (Sandbox Code Playgroud)

下面是一个示例,每次按空格时打印“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");            
    }
}
Run Code Online (Sandbox Code Playgroud)

改为这样做:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)