Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Double jumping with AddForce not working properly

Discussion in 'Physics' started by danielg2502, Nov 13, 2020.

  1. danielg2502

    danielg2502

    Joined:
    Oct 3, 2020
    Posts:
    1
    i'm using addforce to jump from the ground and it works great, but when i try to implement the addforce for the double jump the jump is a lot shorter. i'm doing a high jump too so you jump higher the longer you press the jump button, maybe that has something to do with it idk.
    i'm using a state machine to move between IdleState which is the following:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class IdleState : FSMState
    6. {
    7.     public override void EnterState(PlayerController player){
    8.         player.jumpCount = player.character.jumpCount;//reset jump count
    9.         player.Rbody.gravityScale = 1f;//grounded
    10.         player.Rbody.velocity = Vector2.zero;
    11.     }
    12.  
    13.     public override void Update(PlayerController player){
    14.         //jump
    15.         if(player.pressedJump){
    16.             player.Rbody.AddForce(new Vector2(0, player.character.jumpSpeed), ForceMode2D.Impulse);
    17.             player.TransitionToState(player.JumpState);
    18.         }
    19.  
    20.         //walk
    21.         if(player.velocityX != 0)
    22.             player.TransitionToState(player.WalkState);
    23.     }
    24.  
    25.     public override void FixedUpdate(PlayerController player){
    26.  
    27.     }
    28.  
    29.     public override void OnCollisionStay2D(PlayerController player, Collision2D collision){
    30.  
    31.     }
    32.  
    33. }
    34.  
    JumpState:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class JumpState : FSMState
    6. {
    7.     public override void EnterState(PlayerController player){
    8.         player.Rbody.velocity = new Vector2(0, player.Rbody.velocity.y);//make sure not walking
    9.         player.pressedJump = false;
    10.     }
    11.  
    12.     public override void Update(PlayerController player){
    13.         if(!player.holdingJump || player.Rbody.velocity.y <= 0){
    14.             player.Rbody.velocity = new Vector2(player.Rbody.velocity.x, 0);
    15.             player.TransitionToState(player.FallState);
    16.         }
    17.  
    18.         if(player.holdingJump && player.velocityX != 0)
    19.             player.TransitionToState(player.WJumpState);
    20.     }
    21.  
    22.     public override void FixedUpdate(PlayerController player){
    23.        
    24.     }
    25.  
    26.     public override void OnCollisionStay2D(PlayerController player, Collision2D collision){
    27.        
    28.     }
    29. }
    30.  
    and FallState:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FallState : FSMState
    6. {
    7.     public override void EnterState(PlayerController player){
    8.         player.Rbody.velocity = new Vector2(0, player.Rbody.velocity.y);//make sure not walking
    9.         player.holdingJump = false;
    10.     }
    11.  
    12.     public override void Update(PlayerController player){
    13.         if(player.velocityX != 0)
    14.             player.TransitionToState(player.WFallState);
    15.     }
    16.  
    17.     public override void FixedUpdate(PlayerController player){
    18.         if(player.pressedJump){
    19.             player.Rbody.gravityScale = 1f;
    20.             player.Rbody.velocity = Vector2.zero;
    21.             player.Rbody.AddForce(new Vector2(0, player.character.jumpSpeed), ForceMode2D.Impulse);
    22.             player.TransitionToState(player.JumpState);
    23.         }
    24.  
    25.         if(player.pressingDown)
    26.             player.Rbody.gravityScale = player.character.spcFallGrav;//if special falling
    27.         else
    28.             player.Rbody.gravityScale = player.character.fallGrav;//if falling
    29.     }
    30.  
    31.     public override void OnCollisionStay2D(PlayerController player, Collision2D collision){
    32.         if(collision.collider.CompareTag("Ground"))
    33.             player.TransitionToState(player.IdleState);
    34.     }
    35. }
    36.  
    jumpstate transitions to fallstate when the velocity.y of the player is smaller than 0 (when falling) or when the player stops holding the jump button (i wanted to do the jumping like in hollow knight)

    fallstate transitions to jumpstate when the jump button is pressed again, but for some reason that jump is a lot shorter than the previous.