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

}  

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

Google 직원 이이 문제를 발견하면 다음 코드를 참조하십시오.

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

문제는이다 FixedUpdateUpdate하나씩 호출되지 않습니다. 업데이트는 프레임 당 한 번 호출되고 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;
    }
}
2 EricPendergast Aug 30 2020 at 01:35

한 가지 가능한 문제는 프로젝트가 새 입력 시스템 패키지를 사용하고있을 수 있다는 것 입니다. 이 패키지를 사용하는 경우 이전 입력 관리자 기능이 작동하지 않습니다. 이를 확인하려면로 이동 Edit > Project Settings... > Player > Other Settings하고 Active Input Handling설정해야합니다 Input Manager (Old)(또는 Both수도 또한 작업).

실제로 입력 시스템 패키지를 사용하려면 아직 설치 하지 않은 경우 설치 해야하며 다음과 같이 스페이스 바를 눌렀는지 확인합니다.

using UnityEngine.InputSystem;

...

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

여기에서 문서를 확인하십시오. https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html

사용자가 이름으로 식별되는 키를 누르기 시작하는 프레임 동안 true를 반환합니다.

상태가 매 프레임마다 재설정되므로 Update 함수에서이 함수를 호출해야합니다. 사용자가 키를 놓았다가 다시 누를 때까지 true를 반환하지 않습니다.

GetKey대신 함수를 사용하십시오 .https://docs.unity3d.com/ScriptReference/Input.GetKey.html

사용자가 이름으로 식별되는 키를 누르고있는 동안 true를 반환합니다.