bsekula
bsekula

Reputation: 919

Unity Input.GetButtonDown("Jump") and Input.GetKeyDown(KeyCode.Space) is always false

I cannot detect if space-bar key was pressed, I have following code:

void Update() {
  // Working
  if (Input.anyKeyDown) {
    Debug.Log("anyKeyDown");
  }

  // Not working
  if (Input.GetButtonDown("Jump")) {
    Debug.Log("GetButtonDown - Jump");
  }

  // Not working
  if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown("space")) {
    Debug.Log("KeyCode.Space");
  }

  // Working
  if (Input.GetKeyDown(KeyCode.A)) {
    Debug.Log("A");
  }
}

I can detect if e.g. A was clicked but its not working for Input.GetButtonDown("Jump") or for Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown("space")

Below you can see image from my console:

enter image description here

I am using default unity Input Manager settings, I can see two Jumps configured:

enter image description here enter image description here

Upvotes: 1

Views: 14279

Answers (3)

try using CrossPlatformInputManager

if (CrossPlatformInputManager.GetButton("Space")) {
    Debug.Log("Jump pressed");
}
  • in Input Manager you can add a secondary button for the same input, like your "joystick button 3" on the first Space input as "Alt Positive Button" (Alternative), so no need to have 2 Space Inputs there.
  • why on one Space input you have the "Axis" as "Y Axis" and on the other as "X Axis"? use "X Axis" for both instead.

Upvotes: 0

fastbyte22
fastbyte22

Reputation: 66

Maybe do not use 2 Jumps declarations in the input editor. Make one and select its axis to X.

if (Input.GetKeyDown(KeyCode.Space))
{

}

Upvotes: 1

Joep Eijkemans
Joep Eijkemans

Reputation: 51

Have you considered using

if (Input.GetKeyDown("space"))
    {
        Debug.Log("space key was pressed");
    }

instead?

Upvotes: 0

Related Questions