Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

jumping randomly too high problem

Discussion in 'Scripting' started by unity_I3tE2nCOrWRN3Q, Jan 11, 2020.

  1. unity_I3tE2nCOrWRN3Q

    unity_I3tE2nCOrWRN3Q

    Joined:
    Dec 29, 2019
    Posts:
    2
    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?

    2. 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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityStandardAssets.CrossPlatformInput;
    5.  
    6. public class PlayerControl : MonoBehaviour
    7. {
    8.  
    9.     Animator anim;
    10.     Rigidbody2D rb;
    11.     [SerializeField]
    12.     float jumpForce = 500f;
    13.     float upOrDown;
    14.  
    15.     // Use this for initialization
    16.     void Start()
    17.     {
    18.         rb = GetComponent<Rigidbody2D>();
    19.         anim = GetComponent<Animator>();
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         if (GameControl.gameStopped != true)
    26.         {
    27.             upOrDown = CrossPlatformInputManager.GetAxisRaw("Vertical");
    28.             if (upOrDown > 0 && rb.velocity.y == 0)
    29.                 rb.AddForce(Vector2.up * jumpForce);
    30.  
    31.             if (upOrDown < 0 && rb.velocity.y == 0)
    32.                 anim.SetBool("isDown", true);
    33.             else
    34.                 anim.SetBool("isDown", false);
    35.         }
    36.         if (GameControl.gameStopped != true)
    37.         {
    38.             upOrDown = CrossPlatformInputManager.GetAxisRaw("Vertical");
    39.             if (upOrDown > 0 && Mathf.Abs(rb.velocity.y) == 0)
    40.                 rb.AddForce(Vector2.up * jumpForce);
    41.  
    42.             if (upOrDown > 0)
    43.                 anim.SetBool("isJump", true);
    44.             else
    45.                 anim.SetBool("isJump", false);
    46.  
    47.         }
    48.     }
    49. }
    50.  
    Thank you in advance for your help!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    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."
     
  3. VSM2018

    VSM2018

    Joined:
    Jun 14, 2018
    Posts:
    16
    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.
     
    ijmmai and Klimka13 like this.
  4. AirAura01

    AirAura01

    Joined:
    Jul 23, 2021
    Posts:
    2
    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
     
  5. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    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.
     
    Kurt-Dekker likes this.