Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Update won't detect my jump input correctly

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading