Unity Input.GetKeyDown (KeyCode.Space) tuşu algılamıyor Basın
Unity öğreniyorum ama tuş baskım tespit edilmiyor
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;
}
}
}
}
nedenJumpPressed her zaman yanlıştır. Neyi yanlış yapıyorum? Anladığım kadarıyla bu işe yaramalı ama belli ki bir şeyleri kaçırıyorum
GÜNCELLEME: Fikir öneren herkese teşekkürler. Boşluk çubuğunu algılamayı bıraktığımda isJumpPressed true değerini döndürdüm.
isJumpPressed = Input.GetKeyDown("a");
bunun neden işe yarayıp yaramayacağı konusunda herhangi bir fikri olan
isJumpPressed = Input.GetKeyDown(KeyCode.Space);
veya
isJumpPressed = Input.GetKeyDown("space");
GÜNCELLEME 2: Görünüşe göre bu Linux'ta bir hata. Oyun sadece düzenleyicide kurulduğunda bunun olmayacağını okudum. Bir çözüm buldumhttps://forum.unity.com/threads/space-not-working.946974/?_ga=2.25366461.1247665695.1598713842-86850982.1598713842#post-6188199
Bu sorunla ilgili herhangi bir Google çalışanı Tökezlediğinde, aşağıdaki koda bakın çünkü bu benim için çalışıyor.
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;
}
}
Yanıtlar
Sorun şu ki FixedUpdateve Updatebirbiri ardına çağrılmıyor. Güncelleme çerçeve başına bir kez ve FixedUpdate fizik güncellemesi başına bir kez çağrılır (varsayılan saniyede 50 güncellemedir).
Böylece aşağıdakiler olabilir:
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
Burada, boşluğa her bastığınızda "Güncelle Atlama" ve yalnızca boşluk tuşuna bastığınızda "Sabit Güncelleme Atlama" yazan bir örnek verilmiştir. Bunu yapma:
bool isJumpPressed;
void Update()
{
isJumpPressed = Input.GetKeyDown(KeyCode.Space);
if (isJumpPressed)
{
Debug.Log("Update Jump");
}
}
private void FixedUpdate()
{
if (isJumpPressed)
{
Debug.Log("FixedUpdate Jump");
}
}
Bunun yerine şunu yapın:
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;
}
}
Olası bir sorun, projenizin yeni Girdi Sistemi paketini kullanıyor olabileceğidir . Bu paketi kullanıyorsanız, eski Giriş Yöneticisi işlevleri çalışmayacaktır. Bunu kontrol etmek için gidin Edit > Project Settings... > Player > Other Settingsve Active Input Handlingolarak ayarlanmalıdır Input Manager (Old)(veya Bothişe yarayabilir).
Girdi Sistemi paketini gerçekten kullanmak istiyorsanız, zaten sahip değilseniz yüklemeniz gerekir ve boşluk çubuğuna şu şekilde basılıp basılmadığını kontrol edersiniz:
using UnityEngine.InputSystem;
...
jumpPressed = Keyboard.current.space.wasPressedThisFrame;
Belgeleri buradan kontrol edin: https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html
Çerçeve sırasında kullanıcı adla tanımlanan tuşa basmaya başlarken true döner.
Durum her çerçevede sıfırlandığından, bu işlevi Güncelle işlevinden çağırmanız gerekir. Kullanıcı tuşu bırakıp tekrar basana kadar gerçek olmayacaktır.
Bunun GetKeyyerine işlevi kullanın :https://docs.unity3d.com/ScriptReference/Input.GetKey.html
Kullanıcı adıyla tanımlanan anahtarı basılı tuttuğunda doğru döndürür.