我正在学习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中的一个bug。我读到,当游戏只是在编辑器中构建时,它不会发生。我在https://forum.unity.com/threads/space-not-working.946974/?_ga=2.25366461.1247665695.1598713842-86850982.1598713842#post-6188199上找到了一种解决方法
如果任何Googler遇到这个问题,请参考下面的代码,因为这对我来说是有效的。
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;
}
}
发布于 2020-08-30 01:18:55
问题是FixedUpdate
和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
这里有一个例子,每次你按空格键时都会打印“更新跳转”,有时你按空格键时才会打印"FixedUpdate跳转“。不要这样做:
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;
}
}
发布于 2020-08-30 02:35:34
一个可能的问题是您的项目可能正在使用新的Input System包。如果您正在使用此包,则旧的Input Manager函数将不起作用。要检查这一点,请转到Edit > Project Settings... > Player > Other Settings
,Active Input Handling
应该设置为Input Manager (Old)
(或者Both
也可以)。
如果你真的想要使用输入系统包,如果你还没有的话,你必须install它,并且你可以像这样检查空格键是否被按下了:
using UnityEngine.InputSystem;
...
jumpPressed = Keyboard.current.space.wasPressedThisFrame;
https://stackoverflow.com/questions/63649028
复制相似问题