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