Unity Input.GetKeyDown (KeyCode.Space) ตรวจไม่พบปุ่มกด

Aug 30 2020

ฉันกำลังเรียนรู้ 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;
            }
        }
    }

}  

ทำไม JumpPressed เป็นเท็จเสมอ ผมทำอะไรผิดหรือเปล่า? จากสิ่งที่ฉันเข้าใจสิ่งนี้น่าจะใช้ได้ แต่เห็นได้ชัดว่าฉันพลาดบางอย่าง

UPDATE: ขอบคุณทุกคนที่เสนอไอเดีย ฉันได้รับ isJumpPressed เพื่อส่งคืนจริงเมื่อฉันหยุดพยายามตรวจจับแถบพื้นที่

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 พบปัญหานี้ให้อ้างอิงรหัสต่อไปนี้เนื่องจากสิ่งนี้ใช้ได้ผลสำหรับฉัน

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;
    }

} 

คำตอบ

2 Pluto Aug 30 2020 at 00:18

ปัญหาคือ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 

นี่คือตัวอย่างที่พิมพ์ "Update Jump" ทุกครั้งที่คุณกดเว้นวรรคและ "FixedUpdate Jump" เฉพาะบางครั้งเมื่อคุณกดเว้นวรรค อย่าทำอย่างนี้:

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;
    }
}
2 EricPendergast Aug 30 2020 at 01:35

ปัญหาหนึ่งที่เป็นไปได้คือโครงการของคุณอาจใช้แพ็คเกจระบบป้อนข้อมูลใหม่ หากคุณใช้แพ็คเกจนี้ฟังก์ชันตัวจัดการอินพุตแบบเก่าจะไม่ทำงาน ในการตรวจสอบสิ่งนี้ให้ไปที่Edit > Project Settings... > Player > Other SettingsและActive Input Handlingควรตั้งค่าเป็นInput Manager (Old)(หรือBothอาจใช้งานได้เช่นกัน)

หากคุณต้องการใช้แพ็คเกจระบบอินพุตจริงๆคุณต้องติดตั้งหากคุณยังไม่มีและคุณจะตรวจสอบว่ามีการกด Spacebar หรือไม่:

using UnityEngine.InputSystem;

...

jumpPressed = Keyboard.current.space.wasPressedThisFrame;
AthanasiosKataras Aug 29 2020 at 23:04

ตรวจสอบเอกสารที่นี่: https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html

ส่งกลับค่าจริงระหว่างเฟรมที่ผู้ใช้เริ่มกดคีย์ที่ระบุโดยชื่อ

คุณต้องเรียกใช้ฟังก์ชันนี้จากฟังก์ชันอัปเดตเนื่องจากสถานะจะรีเซ็ตแต่ละเฟรม มันจะไม่กลับมาเป็นจริงจนกว่าผู้ใช้จะปล่อยคีย์และกดอีกครั้ง

ใช้GetKeyฟังก์ชันแทน:https://docs.unity3d.com/ScriptReference/Input.GetKey.html

ส่งคืนจริงในขณะที่ผู้ใช้กดคีย์ที่ระบุโดยชื่อ