c# - unity Input.GetKeyDown(KeyCode.Space) 未检测到按键按下

标签 c# unity3d

我正在学习 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 总是假的。我究竟做错了什么?据我所知,这应该可行,但我显然错过了一些东西
更新:
感谢所有提出想法的人。当我停止尝试检测空格键时,我让 isJumpPressed 返回 true。
isJumpPressed = Input.GetKeyDown("a");
任何人都知道为什么这会起作用而不是
isJumpPressed = Input.GetKeyDown(KeyCode.Space);
或者
isJumpPressed = Input.GetKeyDown("space");
更新 2:
显然这是Linux中的一个错误。我读过当游戏仅在编辑器中构建时它不会发生。我找到了一个解决方法
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;
    }

} 

最佳答案

问题是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 
这是一个每次按空格键时打印“更新跳转”的示例,只有在您按空格键时才打印“固定更新跳转”。不要这样做:
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;
    }
}

关于c# - unity Input.GetKeyDown(KeyCode.Space) 未检测到按键按下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63649028/

相关文章:

c# - 如何在GridView中显示第三列中两列的乘积?

c# - 解析几乎格式正确的 XML 片段 : how to skip over multiple XML headers

ios - unity iOS link.xml 位置

c# - Unity3d:CommandInvokationFailure:Gradle构建失败

c# - URL中的@字符在iOS的Unity3D中导致错误

c# - 如何验证本地化是否在 Bot 框架中正常工作

c# - ASP.Net Video 中的 DataList 应在点击时放大

unity3d - 自升级到play-services-analytics-9.4.0.aar以来,Google Analytics(分析)Unity抛出ClassNotFound

android - 如何从apk提取统一游戏的声音?

c# - Page.Validate 与 Page.IsValid