Search Unity

Question how to ignore falling force when double jumping

Discussion in 'Scripting' started by maxpanic, Jan 25, 2022.

  1. maxpanic

    maxpanic

    Joined:
    Dec 18, 2021
    Posts:
    5
    hello
    The double jump section in my character controller script works fine when the character is moving upwards or at the peak of their jump, but my problem is that when it's falling it adds upward force to downward force so the character just kinda stops midair for a bit. How do you make it jump normal height, ignoring the fact that it's falling?

    character controller:
    Code (CSharp):
    1. [SerializeField] private float speed;
    2.     [SerializeField] private Transform groundCheckTransform = null;
    3.     [SerializeField] private float jumpHeight;
    4.     [SerializeField] private LayerMask playerMask;
    5.     private Rigidbody rb;
    6.     private float xInput;
    7.     private bool jumpKeyWasPressed;
    8.     private bool doubleJumpEnabled;
    9.     private bool doubleJumpPressed;
    10.     private bool doubleJumpPressedThisJump;
    11.  
    12.     void Awake()
    13.     {
    14.         rb = GetComponent<Rigidbody>();
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         xInput = Input.GetAxis("Horizontal");
    20.  
    21.         if (Input.GetKeyDown(KeyCode.Space)) {
    22.             jumpKeyWasPressed = true;
    23.         }
    24.  
    25.         if (Input.GetKeyDown(KeyCode.Space) && (Physics.OverlapSphere(groundCheckTransform.position, 0.1f, playerMask).Length == 0) && doubleJumpEnabled && doubleJumpPressed == false && doubleJumpPressedThisJump == false) {
    26.             doubleJumpPressed = true;
    27.         }
    28.     }
    29.     private void FixedUpdate() {
    30.         rb.velocity = new Vector3 (xInput * speed, rb.velocity.y, 0);
    31.  
    32.         if (Physics.OverlapSphere(groundCheckTransform.position, 0.1f, playerMask).Length == 0 && doubleJumpPressed) {
    33.  
    34.         Debug.Log("Double Jump!");
    35.             doubleJumpPressed = false;
    36.             doubleJumpPressedThisJump = true;
    37.             rb.AddForce(Vector3.up * jumpHeight, ForceMode.Impulse);
    38.         }
    39.  
    40.         if (Physics.OverlapSphere(groundCheckTransform.position, 0.1f, playerMask).Length == 1) {
    41.             doubleJumpPressedThisJump = false;
    42.         }
    43.  
    44.         if (Physics.OverlapSphere(groundCheckTransform.position, 0.1f, playerMask).Length == 0) {
    45.                 return;
    46.         }
    47.  
    48.         if (jumpKeyWasPressed) {
    49.             Debug.Log("Jump!");
    50.             jumpKeyWasPressed = false;
    51.             rb.AddForce(Vector3.up * jumpHeight, ForceMode.Impulse);
    52.         }
    53.     }
    54.  
    55.     private void OnTriggerEnter(Collider other) {
    56.         if (other.gameObject.layer == 6) {
    57.             Destroy (other.gameObject);
    58.             doubleJumpEnabled = true;
    59.         }
    60.     }
    61. }
    62.  
    Thanks in advance
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,860
    I've always just zeroed out the y component of the rigid body's velocity before you apply the jump force.

    Basically:
    Code (CSharp):
    1. rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
    2. rb.AddForce(Vector3.up * jumpHeight, ForceMode.Impulse);
     
    PraetorBlue and maxpanic like this.
  3. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,997
    You could probably even skip AddForce:
    Code (CSharp):
    1. Vector3 newVel=rb.velocity; newVel.y=jumpHeight;
    2. rb.velocity=newVel;