Sometimes my character's controls doesn't respond (lag?)

Hello everyone, I am quite new to Unity, and I had recently attempted to recreate the Google T-rex Runner based on this. I set the character to jump whenever the I press the “Space” key on the keyboard. However sometimes the character doesn’t jump until I pressed two or more times, causing the character to crash into obstacles. I assumed that it is a lag, but does anyone know what is causing this? Feel free to ask me questions, Thank you!

The Javascript for jumping:

#pragma strict
var jumpHeight=5;
var falling=false;

function Start () {
falling=true;
PlayerPrefs.SetInt("speed",10);	
}

function Update () {

transform.Translate(Vector3.right*PlayerPrefs.GetInt("speed",6)*Time.deltaTime);
if(Input.GetKeyDown(KeyCode.Space)&&falling==false){
GetComponent.<Rigidbody2D>().velocity.y=jumpHeight;

}
falling=true;

}

function OnCollisionStay2D() {
falling=false;

}

Use .addforce instead of velocity. Looks like you are using triggers for the jumping, it is very intermittent to work right like that. Use circlecast to see if you are touching the ground layer instead. The unity docs have nice examples. I’m a c# guy so I’m perfect with javascript syntex so Ivan give examples