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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Scripting Double Jump problems

Discussion in 'Scripting' started by ChrisX, Mar 20, 2015.

  1. ChrisX

    ChrisX

    Joined:
    Feb 13, 2015
    Posts:
    63
    Hello all.

    I'd like to ask about double jumping here. Now, I know it's more about letting you do jump twice. However, when the game object is descending, the double jump force gets lessened, weaker than when I just tell the object to jump before it started descending. I want to make a consistent double jumping. Here's my code so far for jump:

    Code (CSharp):
    1. if (stateJump)
    2. rigidbody2D.addForce(new Vector2 (0, 200*Time.deltaTime));
    (stateJump being a state where it's OK to jump, like if you press up arrow.)

    Where do you think I go wrong?

    Thanks for the advice.
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    You need to add force proportional to the existing vertical force, alternatively just set set the vertical speed directly.
     
  3. ChrisX

    ChrisX

    Joined:
    Feb 13, 2015
    Posts:
    63
    Well I could use some help in doing that. I'm very much a newbie in that.

    Right now I didn't do anything to the gravity force.
     
  4. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    Don't use delta time on physics, just do the physics in the fixed update
     
  5. Roderyk

    Roderyk

    Joined:
    Mar 5, 2015
    Posts:
    75
    for jumping.

    Code (CSharp):
    1. //Get the rigidBody component
    2. RigidBody myRigidBody = GetComponent<RigidBody>();
    3. //Set a jumpHeight variable
    4. public float jumpHeight = 10f;
    5.  
    6. myRigidBody.velocity = Vector3.up * jumpHeight;
     
  6. ChrisX

    ChrisX

    Joined:
    Feb 13, 2015
    Posts:
    63
    That works wonders. Another alternative is everytime you jump, you set your y velocity to 0 first before adding force.

    Should velocity fall into Fixed update too?
     
  7. Roderyk

    Roderyk

    Joined:
    Mar 5, 2015
    Posts:
    75
    Yes. But velocity fall is based on gravity. So, you let physics do it thing with gravity (or just give more force to the gravity itself), or you can disable gravity and manage yourself the falling.