Search Unity

Calculate Horizontal Jump Velocity

Discussion in 'Physics' started by shotoutgames, Apr 28, 2016.

  1. shotoutgames

    shotoutgames

    Joined:
    Dec 29, 2013
    Posts:
    290
    Hi all
    2.5D setup.
    When player jumps I set a vector and supply a Y velocity and a X velocity (horizontal force) each frame in Fixed Update. I would like to know with a fixed Y how to calculate X so I get a hyperbolic jump instead of the Zig Zag type of jump I have now. I experiment with different jumpHorizontal force values but I would like a way with a formula to calculate this given a jump vertical force and height. and gravity (custom value that I'm using)

    Thanks


    Code (CSharp):
    1.  if (!grounded) {
    2.  
    3.             anim.SetBool("Grounded", grounded);
    4.             float jhf = leftStickX * jumpStats.jumpHorizontalForce;
    5.             Vector3 jumpDirection = new Vector3( jhf, verticalJumpForce, leftStickY * jumpStats.jumpHorizontalForce);
    6.             GetComponent<Rigidbody>().velocity = new Vector3(0f, GetComponent<Rigidbody>().velocity.y, GetComponent<Rigidbody>().velocity.z);
    7.             GetComponent<Rigidbody>().AddForce(jumpDirection, ForceMode.Force);
    8.         }
     
  2. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Your formula is very complicated, it's unlike any jump i have seen before. Parabolic jump (if i understand what you mean) is something like:
    Code (CSharp):
    1.  
    2. RigidBody rb = GetComponent<Rigidbody>();
    3. anim.SetBool("Grounded", grounded);
    4. if (Input.GetButtonDown("jump") && grounded) {
    5.   Vector3 force = Vector3.up * verticalJumpForce;
    6.   rb.AddForce(force, ForceMode.Impulse);
    7. }
    What is horizontal jump force, in detail and is it necessary? The parabolic looks of a jump comes from character's own movement speed. It is a different matter then how much force the player can use when in air, that is something that can be multiplied with some float AirControl where player is normally moved, when not grounded. Similarly when in air, it might reduce the RigidBody's drag value down a bit to compensate lack of ground friction. Or maybe you handle that with physics materials anyway.
     
  3. shotoutgames

    shotoutgames

    Joined:
    Dec 29, 2013
    Posts:
    290
    Thank you,
    The horizontal Jump Force is so I don't have to rely on the characters movement only to control the horizontal force. I also zero the horizontal velocity 1st to purposely stop the player movement on the ground from influencing the jump. I also use horizontal forces in wall jumps for example. This way it is independent of any movement speed from the player character. It just allows me less unpredictability. Unfortunately it appears to just complicate things :) I am able to control the horizontal movement in the air through variables in script. I was just wondering more so is their some kind of relationship between the horizontal and vertical force that will allow for an arc like jump instead of quick direction changes when using this method.
     
  4. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Well, setting the velocity and using AddForce is kind of same thing. You can try setting velocity to Vector3.zero when jump is first clicked, and add some initial force to it to a direction. Then while flying use only AddForce to control the flight, if that is how intended. But no i don't think there is a relation between height from ground and horizontal position.

    Should there be a relation to how much the controls affect movement based on how long it's been since pressing jump button? That can be done with some timer variable too, but not sure if i know any game that did so.