Home > Mobile >  Update won't detect my jump input correctly
Update won't detect my jump input correctly

Time:01-11

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 Related Function:

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?

CodePudding user response:

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

CodePudding user response:

While your code is correct, I suggest using Rigidbody.AddForce(Vector3 force); or in case you are using rigidbody 2d Rigidbody2D.AddForce(Vector2 force); instead of using r.velocity=.... To make it easier to debug - you can create a public variable for the force and tweak it while in play mode until you get satisfactory results.

  • Related