- Home /
Unity not recognising space bar and left click
Hi all!
Currently making a simple first game in Unity. When the user uses a key, the character should jump (2D game btw). However, the space bar and left click are not registering. When I use anyKeyDown it detects every other key I've tried. The space bar, when used, interacts with Unity instead of the game ie. opens a drop down menu in the game window. Mouse click does something along the same lines. Same happens when use Input.GetKeyDown(KeyCode.Space) and Input.GetMouseButtonDown(0), respectively.
void Update() { if(Input.anyKeyDown) { didFlap = true; } }
Thanks in advance!
Pretty strange. I know this is a stupid question, but did you clicked on the play button? And if possible, show us the script. $$anonymous$$aybe you are missing something.
Answer by paulvincentphillips · Oct 19, 2016 at 09:03 PM
using UnityEngine; using System.Collections;
public class BirdMovement : MonoBehaviour {
Vector3 velocity = Vector3.zero;
public Vector3 gravity;
public Vector3 flapVelocity;
public float maxSpeed = 5f;
bool didFlap = false;
// Use this for initialization
void Start () {
}
// Do Graphic & Input updates here
void Update() {
if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) ) {
didFlap = true;
}
}
// Do physics engine updates here
void FixedUpdate () {
velocity += gravity * Time.deltaTime;
if(didFlap == true) {
didFlap = false;
velocity += flapVelocity;
}
velocity = Vector3.ClampMagnitude(velocity, maxSpeed);
transform.position += velocity * Time.deltaTime;
float angle = 0;
if (velocity.y < 0) {
angle = Mathf.Lerp(0, -90, -velocity.y / maxSpeed);
}
transform.rotation = Quaternion.Euler(0, 0, angle);
}
}
For context: Flappy Bird clone
@paulvincentphillips this may sound stupid, but did you try clicking on the actual game window before pressing the button?
Yeah, play button is pressed. It's as if game runs and when I press space it'll open a pop up window and interrupt the game. Same if I click but it might interact with a sprite etc.
no I mean, do you first click on the actual window where the games is, just on the window. It might be focused on editor still.
Answer by trollent1113 · Oct 19, 2016 at 09:03 PM
if you go to edit---project settings---input it should tell you what you can type for space or ''jump''. if you need a simple 2d jump script see if this works
public float jumpForce = 7.5f;
void Start()
{
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpForce);
}
}
this is not tested btw. Hope it helps @ paulvincentphillips
Answer by PeteD · Oct 20, 2016 at 01:49 PM
Interesting. The behaviour you describe is present if you have toggled sticky keys to be on. In which case pressing space will act as a menu open instead of a standard key press.
If you are on windows check under accessibility, that you don't have sticky keys or any of those settings enabled. Not sure where this setting lives on a mac.
Answer by RecklessTechguy · Aug 31, 2020 at 10:37 PM
I am having the same issue with using space button, though I do not get a menu action from using it. Have not tested this with the left mouse so far. If you find a fix I would love to know.
Your answer
Follow this Question
Related Questions
check if spacebar was pressed two times 2 Answers
Change a Variable on Key Down 5 Answers
Check if button is pressed 1 Answer
KeyCode.Enter 2 Answers