Search Unity

Adding a Rise/Fall Jump to a PlayerController

Discussion in 'Scripting' started by IcemanPantsify, Feb 27, 2017.

  1. IcemanPantsify

    IcemanPantsify

    Joined:
    Feb 24, 2017
    Posts:
    2
    So I'm fairly new to Unity and I was testing out 3D movement. I can add basic movement but I decided to add a jump. The way I figured it out by was to translate the object to the point in the air. However, the code instantly takes the object to the point then drops the object.

    How would you code the object so that when triggered, would rise to the maximum height if the jump key is held, then would fall?

    Current Code:
    public float jumpHeight;

    if (Input.GetKeyDown("space"))
    transform.Translate (Vector3.up * jumpHeight * Time.deltaTime, Space.World);
     
  2. StridingDragon

    StridingDragon

    Joined:
    Jan 16, 2013
    Posts:
    78
    What you want to do is add a force to the rigibody, instead of doing a Translate..

    Code (CSharp):
    1.             _rb.AddForce(  0, gravity, 0, ForceMode.Acceleration );
    In this example, _rb would be the rigidbody component attached to your transform, and gravity is any value you see fit, though most people would use 9.8, as it is properly reflecting Newtonian physics.
     
  3. AndyGainey

    AndyGainey

    Joined:
    Dec 2, 2015
    Posts:
    216
    ForceMode.Acceleration is only appropriate if you continuously add the force each frame as long as the jump key is held down, but this is going to lead to a very awkward jump feel, more like a rocket taking off (slowly at first and speeding up over time) rather than a real-life jump (higher initial velocity slowing down and eventually reversing at the peak).

    I recommend adding an upward impulse (or a mass-ignorant velocity change) only on the first frame that the jump button was pressed, and switch the character to a "jumping" state. In later frames, as long as the button is held down and the character is still moving upward, do nothing special other than letting gravity slowly reverse the upward motion back downward. (If you're using Unity's physics, literally do nothing special; if handling physics yourself, just apply standard gravitational force, with no special upward force due to jumping.) As soon as the character is no longer moving upward at all, switch to a "falling" state. Or, if the player releases the jump key before the character peaks, instantly cap the character's upward velocity to a small constant (zero is usually too small, as it makes it feel like the character just hit their head on an invisible ceiling) and switch to a "falling" state, thereby killing their jump early and giving the player a decent sense of jump control. When in the "falling" state, there's no need to pay attention to the jump key at all, since the player can no longer kill a jump early or start a new jump until they've landed on the ground again (unless/until you implement double jump, that is).
     
  4. StridingDragon

    StridingDragon

    Joined:
    Jan 16, 2013
    Posts:
    78
    Good point, Andy.