C
C
Coffee_yechic2020-09-11 18:17:26
C++ / C#
Coffee_yechic, 2020-09-11 18:17:26

The character sometimes jumps double the height. How to fix it?

The character randomly jumps to double the height from the prescribed one.

Wrote the most standard jump code:

private void Jump() {
        rigidbody.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
}


Also quite common code for checking the ground under your feet:
private void CheckGround() {
        Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, 0.3f);

        isGrounded = colliders.Length > 1;
}


Calling the jump function:
private void Update() {
        if (isGrounded && Input.GetButton("Jump")) Jump();
}


Calling the function to check the ground under your feet:
private void FixedUpdate() {
        CheckGround();
}


Thank you in advance.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
Coffee_yechic, 2020-09-12
@Coffee_yechic

I solved this problem. As it turned out, everything was very simple... I called the jump function in Update, which is strongly discouraged... And along with framerate jumps and drops, the character jumped up and down. After I transferred all the movement to FixedUpdate, the problem capitulated.

G
Griboks, 2020-09-11
@Griboks

Usually this situation occurs due to incorrect ground testing. For example, the player has not yet fallen, but the collider has already worked. Or the player fired a pistol and the bullet was detected as ground. Or a vertical wall hit the collider.
I advise you to rewrite under raycast under your feet. Also check if the impulse works correctly. Maybe your project is better off using speed.

D
dollar, 2020-09-12
@dollar

private void Jump() {
        rigidbody.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
}

Now make it so that a double call to this function would give the same speedup as a single call. The simplest is to limit the character's upward acceleration to some constant. If the acceleration exceeds the threshold, then truncate to this value.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question