首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Unity Input.GetKeyDown(KeyCode.Space)未检测到按键

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

Stack Overflow用户
提问于 2020-08-30 00:00:43
回答 2查看 8K关注 0票数 4

我正在学习Unity,但是我的按键没有被检测到

代码语言:javascript
复制
    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。

代码语言:javascript
复制
isJumpPressed = Input.GetKeyDown("a");

有没有人知道为什么这样能行,为什么不行?

代码语言:javascript
复制
isJumpPressed = Input.GetKeyDown(KeyCode.Space);

代码语言:javascript
复制
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上找到了一种解决方法

如果任何Googler遇到这个问题,请参考下面的代码,因为这对我来说是有效的。

代码语言:javascript
复制
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;
    }

} 
EN

回答 2

Stack Overflow用户

发布于 2020-08-30 01:18:55

问题是FixedUpdateUpdate并没有被一个接一个地调用。每帧调用一次更新,每次物理更新调用一次FixedUpdate (默认值为每秒50次更新)。

因此,可能会发生以下情况:

代码语言:javascript
复制
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 

这里有一个例子,每次你按空格键时都会打印“更新跳转”,有时你按空格键时才会打印"FixedUpdate跳转“。不要这样做:

代码语言:javascript
复制
bool isJumpPressed;
void Update()
{
    isJumpPressed = Input.GetKeyDown(KeyCode.Space);
    if (isJumpPressed)
    {            
        Debug.Log("Update Jump");
    }       
}

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

改为执行以下操作:

代码语言:javascript
复制
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;
    }
}
票数 3
EN

Stack Overflow用户

发布于 2020-08-30 02:35:34

一个可能的问题是您的项目可能正在使用新的Input System包。如果您正在使用此包,则旧的Input Manager函数将不起作用。要检查这一点,请转到Edit > Project Settings... > Player > Other SettingsActive Input Handling应该设置为Input Manager (Old) (或者Both也可以)。

如果你真的想要使用输入系统包,如果你还没有的话,你必须install它,并且你可以像这样检查空格键是否被按下了:

代码语言:javascript
复制
using UnityEngine.InputSystem;

...

jumpPressed = Keyboard.current.space.wasPressedThisFrame;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63649028

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档