Unity Input.GetKeyDown(KeyCode.Space)がキーを検出しないPress

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が常にfalseである理由。私は何が間違っているのですか?私が理解していることから、これはうまくいくはずですが、明らかに何かが欠けています

更新:アイデアを提案してくれたすべての人に感謝します。スペースバーの検出を停止すると、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;
    }

} 

回答

2 Pluto Aug 30 2020 at 00:18

問題は、FixedUpdateUpdateが次々に呼び出されないことです。Updateはフレームごとに1回呼び出され、FixedUpdateは物理更新ごとに1回呼び出されます(デフォルトは1秒あたり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」を出力し、スペースを押すたびに「FixedUpdateJump」を出力する例です。こんなことしないで:

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

考えられる問題の1つは、プロジェクトが新しい入力システムパッケージを使用している可能性があることです。このパッケージを使用している場合、古い入力マネージャー機能は機能しません。これを確認するには、に移動しEdit > Project Settings... > Player > Other SettingsActive 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を返します。