Update won't detect my jump input correctly

Advertisements

I’m following some guides and from what I’ve seen using the update function is a good thing for the jump action, but when I do it the update sends me to a different location.

Jump:

void Update(){
   if(dead == false){
      if(Input.GetKeyDown(KeyCode.Space)){
         if(OnGround == true){
            r.velocity = new Vector2(r.velocity.x, jumpForce * time.deltaTime);
         }
      }
   }
}

From an internet search I found the following, but they didnt help me so much option1 and option2

Mostly it moves me to a different location or jumps me too much high or too much low, never a good proper jump, I really have no idea what to do, any ideas?

>Solution :

You should not be using Time.deltaTime when you’re assigning velocities directly. The value of Time.deltaTime depends on the frame rate, which is expected to jitter, which gets inherited into your jump so that you get a quasi random velocity and jump height.

if(!dead && Input.GetKeyDown(KeyCode.Space) && OnGround){
  r.velocity = new Vector2(r.velocity.x, jumpForce);
}

typically the AddForce function is used for jumps, which is technically the "correct" way to go about it, but I know plenty of people that prefer your method. In any case I’ll just put it here for reference.

if(!dead && Input.GetKeyDown(KeyCode.Space) && OnGround){
  r.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
}

Leave a ReplyCancel reply