- Home /
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;
}
Answer by melsy · Jun 15, 2017 at 05:02 PM
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
Your answer
Follow this Question
Related Questions
Keyboard / Mouse control for FPS 0 Answers
How to change the editor's keyboard to AZERTY layout? 1 Answer
How do you make a method that gets called when a button gets highlighted using the arrow keys? 0 Answers
OnGUI() keyboard input 1 Answer
how make to make ios keyboard's return key submit input? 0 Answers