Unity Input.GetKeyDown(KeyCode.Space) not detecting key Press

爷,独闯天下 提交于 2021-02-05 05:33:24

问题


I'm learning Unity but my key press isn't being detected

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

}  

why isJumpPressed always false. What am I doing wrong? From what I understand this should work but I'm obviously missing something

UPDATE: Thanks to everyone who proposed ideas. I got the isJumpPressed to return true when I stopped trying to detect the space bar.

isJumpPressed = Input.GetKeyDown("a");

anyone got any ideas why this would work and not

isJumpPressed = Input.GetKeyDown(KeyCode.Space);

or

isJumpPressed = Input.GetKeyDown("space");

UPDATE 2: Apparently this is a bug in Linux. I've read it won't happen when the game is built just in the editor. I've found a workaround at https://forum.unity.com/threads/space-not-working.946974/?_ga=2.25366461.1247665695.1598713842-86850982.1598713842#post-6188199

If any Googler's Stumble upon this issue reference the following code because this is working for me.

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

} 

回答1:


The problem is that FixedUpdate and Update are not called one after the other. Update is called once per frame and FixedUpdate is called once per physics update (default is 50 updates per second).

So the following could happen:

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 

Here is an example that prints "Update Jump" every time you press space, and "FixedUpdate Jump" only sometimes when you press space. Do not do this:

bool isJumpPressed;
void Update()
{
    isJumpPressed = Input.GetKeyDown(KeyCode.Space);
    if (isJumpPressed)
    {            
        Debug.Log("Update Jump");
    }       
}

private void FixedUpdate()
{
    if (isJumpPressed)
    {
        Debug.Log("FixedUpdate Jump");            
    }
}

Do this instead:

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:


One possible problem is that your project might be using the new Input System package. If you are using this package, the old Input Manager functions will not work. To check this, go to Edit > Project Settings... > Player > Other Settings and Active Input Handling should be set to Input Manager (Old) (or Both might also work).

If you actually want to use the Input System package, you have to install it if you don't already have it, and you would check if the spacebar is pressed like so:

using UnityEngine.InputSystem;

...

jumpPressed = Keyboard.current.space.wasPressedThisFrame;



回答3:


Check the docs here: https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html

Returns true during the frame the user starts pressing down the key identified by name.

You need to call this function from the Update function, since the state gets reset each frame. It will not return true until the user has released the key and pressed it again.

Use the GetKey function in stead: https://docs.unity3d.com/ScriptReference/Input.GetKey.html

Returns true while the user holds down the key identified by name.



来源:https://stackoverflow.com/questions/63649028/unity-input-getkeydownkeycode-space-not-detecting-key-press

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!