gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:04:13 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com