'How to make jump faster when player is running?

I have a character controller movement where the player can walk, run and jump. However, I facing an issue with the jumping. When jumping while walking, the jump speed looks good. But when jumping while running, the jump speed is too slow compared to the run speed. How can I fix this?

Here is my code:

public class PlayerMovement : MonoBehaviour
{
    [SerializeField] Transform playerCamera = null;
    [SerializeField] float mouseSensitivity = 3.5f;
    [SerializeField] float walkSpeed = 10.0f;
    [SerializeField] float RunSpeed = 12.0f;
    [SerializeField] float gravity = 9.81f;
    [SerializeField] bool lockCursor = true;
    [SerializeField] [Range(0.0f, 0.5f)] float moveSmoothTime = 0.3f;
    public float jumpHeight = 3f;

    Vector3 velocity;
    public float verticalVelocity;
    float cameraPitch = 0.0f;

    CharacterController controller = null;
    Vector2 currentDir = Vector2.zero;
    Vector2 currentDirVelocity = Vector2.zero;
    Vector2 currentMouseDelta = Vector2.zero;
    Vector2 currentMouseDeltaVelocity = Vector2.zero;

    void Start()
    {
        controller = GetComponent<CharacterController>();
        if (lockCursor)
        {
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;
        }
    }
    void Update()
    {
        UpdateMouseLook();
        UpdateMovement();
    }
    void UpdateMouseLook()
    {
        Vector2 targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));

        currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, mouseSmoothTime);

        cameraPitch -= currentMouseDelta.y * mouseSensitivity;

        cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);

        playerCamera.localEulerAngles = Vector3.right * cameraPitch;

        transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity);
    }
    void UpdateMovement()
    {
        Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
        targetDir.Normalize();

        currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, moveSmoothTime);

        if (controller.isGrounded)
            velocity.y = 0.0f;
        velocity += (velocity.y < 0 ? Physics.gravity * 2 : Physics.gravity) * Time.deltaTime;

        velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * walkSpeed + Vector3.up * velocity.y;
          if (Input.GetKeyDown(KeyCode.J) && controller.isGrounded)
        {
           velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
           velocity += (velocity.y < 0 ? Physics.gravity * 2 : Physics.gravity) * Time.deltaTime;
        }
        controller.Move(velocity * Time.deltaTime);
        if ((Input.GetKey("left shift") || Input.GetKey("right shift")) && controller.isGrounded && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.DownArrow))
        {
            velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * RunSpeed + Vector3.up * velocity.y;
            controller.Move(velocity * Time.deltaTime);
        }

      
    

}



Solution 1:[1]

I optimized the above code and removed some dublicate. I also noticed that the speed problem was due to the presence of isGrounded in the body of the running condition, which made running dependent on being on the ground. This is the player script:

   public class Player : MonoBehaviour
    {
        [SerializeField] Transform playerCamera = null;
        [SerializeField] float mouseSensitivity = 3.5f;
        [SerializeField] float walkSpeed = 10.0f;
        [SerializeField] float runSpeed = 12.0f;
        [SerializeField] float gravity = 9.81f;
        [SerializeField] bool lockCursor = true;
        [SerializeField] [Range(0.0f, 0.5f)] float moveSmoothTime = 0.3f;
        public float jumpHeight = 3f;
    
        Vector3 velocity;
        private float verticalVelocity;

        private Vector3 inputVector;
        float cameraPitch = 0.0f;
    
        CharacterController controller = null;
        Vector2 currentDir = Vector2.zero;
        Vector3 currentDirVelocity = Vector3.zero;
        Vector2 currentMouseDelta = Vector2.zero;
        Vector2 currentMouseDeltaVelocity = Vector2.zero;

        void Start()
        {
            controller = GetComponent<CharacterController>();
            
            if (lockCursor)
            {
                Cursor.lockState = CursorLockMode.Locked;
                Cursor.visible = false;
            }
        }
        void Update()
        {
            UpdateMouseLook();
            UpdateMovement();
        }
        void UpdateMouseLook()
        {
            var targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
    
            currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, moveSmoothTime);
    
            cameraPitch -= currentMouseDelta.y * mouseSensitivity;
    
            cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);
    
            playerCamera.localEulerAngles = Vector3.right * cameraPitch;
    
            transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity);
        }

        void UpdateMovement()
        {
            // jumping
            var isGrounded = Physics.Raycast(transform.position, Vector3.down, 1.1f) && velocity.y <= 0;

            if (isGrounded) 
            {
                velocity.y = Input.GetKeyDown(KeyCode.Space) ? Mathf.Sqrt(jumpHeight) : 0f;
            }
            else
            {
                velocity += (velocity.y < 0 ? Physics.gravity * 2 : Physics.gravity) * Time.deltaTime;
            }
            
            controller.Move(velocity);
            
            // movement
            inputVector = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).normalized;

            var smoothDamp = Vector3.SmoothDamp(inputVector, inputVector, ref currentDirVelocity, moveSmoothTime);

            var runKey = (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) &&
                         !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.DownArrow);

            controller.Move(smoothDamp * (runKey ? runSpeed : walkSpeed) * Time.deltaTime);
        }
    }

Also, these settings were suitable for a height 2 capsule:

enter image description here

If your character is greater than 2, adjust the length of the ground detection raycast accordingly because the controller.isGrounded condition does not always work.

enter image description here

Solution 2:[2]

When you are using character controller you need to do all the calculations for all the situations. To get the result you are looking for you can do a calculation beetween jump and walking speed.

Another way is to use Rigid body (lock rotations) and add forces for jump and move.

both examples here: https://www.youtube.com/watch?v=b1uoLBp2I1w

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Daniel Pirvu