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

2D Character Randomly Jumping High

Discussion in 'Physics' started by YBtheS, Sep 11, 2018.

  1. YBtheS

    YBtheS

    Joined:
    Feb 22, 2016
    Posts:
    239
    I have a game object with Platformer Character 2D script that comes in Unity's standard assets. This game object has a rigidbody and is AI controlled. I have another game object with the Platformer Character 2D script that is player controlled. For some reason, the AI controlled one jumps much higher most of time but not always even though the Jump Force variable in the inspector is set to 1200 for both. They both have the same rigidbody mass so why does this happen?
     
  2. YBtheS

    YBtheS

    Joined:
    Feb 22, 2016
    Posts:
    239
    After doing some testing I've figured out that for some reason, my AI character is performing its initial jump and then moments later, jumping again even though it's in the air. I don't see why though. I haven't made any changes to the Platformer Character 2D script as far as I can tell...
     
  3. StickyHoneybuns

    StickyHoneybuns

    Joined:
    Jan 16, 2018
    Posts:
    207
    I have never used the platform character 2d script so I can't comment on what exactly is happening. However, double jumping is a common issue with lots of jumping code. If the code isn't too long, post it here so I can possible see what's going on.

    Do you know how its detecting the ground? How does the script know when its in the air?
     
  4. MoxenGames

    MoxenGames

    Joined:
    Oct 20, 2013
    Posts:
    8
    I've had this problem when a character jumps next to another character. I thought it was just my custom-built hacky mess, but I recently tried Corgi Engine and it turns out this issue exists there as well, which unless I'm mistaken doesn't even rely on the physics engine.
     
  5. UnknownGameDev123

    UnknownGameDev123

    Joined:
    Nov 17, 2019
    Posts:
    5
    I am 2 years late, but just in case someone else comes here.

    I've had this problem. If you are controlling the Rigidbody character by setting it's velocity, then do not set the velocity in the FixedUpdate() mathod. Do it in Update() method only. It helped me.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Sorry but this is bad advice. The velocity of a Rigidbody2D isn't even used until the simulation runs which is every fixed update by default.
     
  7. YBtheS

    YBtheS

    Joined:
    Feb 22, 2016
    Posts:
    239
    Using AddForce is probably a safer option that setting velocity.
     
  8. Mr_Mendel

    Mr_Mendel

    Joined:
    Dec 31, 2013
    Posts:
    19
    I know this is an old post but people may still come here. I mean I did at first.

    At the top create these 2 variables. JumpDelay which can be changed it is the minimum time allowed between a jump.


    [SerializeField] float JumpDelay = 0.5f;
    private bool CanJump = true;

    Changed this function below and add my one underneath it.

    public void Move(float move, bool crouch, bool jump)
    {
    ......
    ......

    // If the player should jump...
    if (m_Grounded && jump && CanJump)
    {
    CanJump = false;
    Invoke("ResetJump", JumpDelay);
    // Add a vertical force to the player.
    m_Grounded = false;
    m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
    }
    }

    private void ResetJump() { CanJump = true; }
     
    YBtheS likes this.