Unity Input.GetKeyDown(KeyCode.Space)未检测到按键 [英] Unity Input.GetKeyDown(KeyCode.Space) not detecting key Press
问题描述
我正在学习Unity,但是没有检测到按键状态
使用System.Collections;使用System.Collections.Generic;使用UnityEngine;公共类玩家:MonoBehaviour{公共刚体myBody;私有浮动时间= 0.0f;私人布尔isMoving = false;私人布尔isJumpPressed = false;无效的Start(){myBody = GetComponent< Rigidbody>();}无效Update(){isJumpPressed = Input.GetKeyDown(KeyCode.Space);Debug.Log(isJumpPressed);}无效FixedUpdate(){if(isJumpPressed){myBody.velocity = new Vector3(0,10,0);isMoving = true;Debug.Log("jump");}if(isMoving){时间=时间+ Time.fixedDeltaTime;如果(时间> 10.0f){//Debug.Log(Debug.Log(gameObject.transform.position.y +``:" + time)));时间= 0.0f;}}}}
为什么isJumpPressed始终为false.我究竟做错了什么?据我了解,这应该可行,但是我显然遗漏了一些东西
更新:感谢所有提出想法的人.当我停止尝试检测空格键时,我得到了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
如果有任何Googler遇到此问题,请参考以下代码,因为它对我有用.
公共类Player:MonoBehaviour{公共刚体myBody;私有浮动时间= 0.0f;私人布尔isMoving = false;私人布尔isJumpPressed = false;无效的Start(){myBody = GetComponent< Rigidbody>();}无效Update(){isJumpPressed = Input.GetKeyDown(SpacebarKey());如果(isJumpPressed){Debug.Log(isJumpPressed);}}无效FixedUpdate(){if(isJumpPressed){myBody.velocity = new Vector3(0,10,0);isMoving = true;Debug.Log("jump");}if(isMoving){时间=时间+ Time.fixedDeltaTime;如果(时间> 10.0f){//Debug.Log(Debug.Log(gameObject.transform.position.y +``:" + time)));时间= 0.0f;}}}公共静态KeyCode SpacebarKey(){如果(Application.isEditor)返回KeyCode.O;否则返回KeyCode.Space;}}
一个可能的问题是您的项目可能正在使用新的输入管理器功能将无法使用.要对此进行检查,请转到 Edit>项目设置...>播放器>其他设置
和活动输入处理
应该设置为输入管理器(旧)
(或两者
可能也可以).>
如果您实际上要使用输入系统软件包,则必须安装(如果您还没有安装的话),那么您将检查是否按下了空格键:
使用UnityEngine.InputSystem;...jumpPressed = Keyboard.current.space.wasPressedThisFrame;
I'm learning Unity but my key press isn't being detected
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;
}
}
}
}
why isJumpPressed always false. What am I doing wrong? From what I understand this should work but I'm obviously missing something
UPDATE: Thanks to everyone who proposed ideas. I got the isJumpPressed to return true when I stopped trying to detect the space bar.
isJumpPressed = Input.GetKeyDown("a");
anyone got any ideas why this would work and not
isJumpPressed = Input.GetKeyDown(KeyCode.Space);
or
isJumpPressed = Input.GetKeyDown("space");
UPDATE 2: Apparently this is a bug in Linux. I've read it won't happen when the game is built just in the editor. I've found a workaround at https://forum.unity.com/threads/space-not-working.946974/?_ga=2.25366461.1247665695.1598713842-86850982.1598713842#post-6188199
If any Googler's Stumble upon this issue reference the following code because this is working for me.
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;
}
}
One possible problem is that your project might be using the new Input System package. If you are using this package, the old Input Manager functions will not work. To check this, go to Edit > Project Settings... > Player > Other Settings
and Active Input Handling
should be set to Input Manager (Old)
(or Both
might also work).
If you actually want to use the Input System package, you have to install it if you don't already have it, and you would check if the spacebar is pressed like so:
using UnityEngine.InputSystem;
...
jumpPressed = Keyboard.current.space.wasPressedThisFrame;
这篇关于Unity Input.GetKeyDown(KeyCode.Space)未检测到按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!