jumping randomly too high problem

Hello Everyone,

My problem has probably already been discussed here before. I found several similar problems, but no solution (if any) did not help in my case.
I’m creating my first endless runner game. There are two problems:

  1. Very often my character randomly jumps too high in the air after the jump, as in the video shown:

Please help me, what is missing in the script of my character (under the second problem) that this jump appears. Is an attribute missing in the script, or some tag in the hierarchy in Unity?

  1. What should be changed in touch control (maybe in this player control script) so that the character doesn’t jump while holding down the button, but requires that you press the jump button (touch the sceen) again?

My character script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

public class PlayerControl : MonoBehaviour
{

    Animator anim;
    Rigidbody2D rb;
    [SerializeField]
    float jumpForce = 500f;
    float upOrDown;

    // Use this for initialization
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (GameControl.gameStopped != true)
        {
            upOrDown = CrossPlatformInputManager.GetAxisRaw("Vertical");
            if (upOrDown > 0 && rb.velocity.y == 0)
                rb.AddForce(Vector2.up * jumpForce);

            if (upOrDown < 0 && rb.velocity.y == 0)
                anim.SetBool("isDown", true);
            else
                anim.SetBool("isDown", false);
        }
        if (GameControl.gameStopped != true)
        {
            upOrDown = CrossPlatformInputManager.GetAxisRaw("Vertical");
            if (upOrDown > 0 && Mathf.Abs(rb.velocity.y) == 0)
                rb.AddForce(Vector2.up * jumpForce);

            if (upOrDown > 0)
                anim.SetBool("isJump", true);
            else
                anim.SetBool("isJump", false);

        }
    }
}

Thank you in advance for your help!

If you want 100% consistent behavior you might want to simulate the jumping arc yourself. Otherwise you may be subject to the minor inconsistencies that happen in the physics engine.

As far as the code above, checking floating point numbers for equality is never a good idea, due to floating point inaccuracies. You ought to use Mathf.Approximately() or else do your own notion of “close enough.”

Since you have buttons in your video for jumping and sliding I would suggest calling those methods when certain buttons are pushed and losing that GetAxes if check. Also make a bool to check whether the player is already jumping. To avoid inconsistent height of the jump before adding the jumpforce set the rb Y velocity to 0.

2 Likes

Try calling all the cases related to jump in the FixedUpdate function instead of Update.
For explanation you may check the answer of NirielNabokov from the following page:
Random high jumps (C#) - Unity Answers

Don’t use addForce for jumps. It’s actually a quite common issue. Your character is probably colliding with the ground for multiple frames and you therefore make it jump basically twice. Setting velocity directly is a quick solution for this.

Edit: Now I noticed that this was a necro. Leaving this here for future reference.

1 Like

Hello everyone, my problem is suddenly jumping out of Unity and entering the report bug page of Unity for no reason, if you can help me

Please don’t necro old threads for a completely unrelatd issue, simply create your own thread!

Thanks.