Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In Brackeys' FPS tutorial, how can I keep momentum when jumping in a specific direction?

This is something I have been confused about for weeks now and it seems that most other places I've looked don't give me the answer I'm looking for. I used his video as a guide but made many changes to achieve a different type of goal but it's mostly the same.

In the current video that Brackeys has, jumping and then letting go of the key will cause the player to abruptly stop mid air and then fall straight downwards. In most FPS games, that never happens. Instead the player will carry their momentum into the jump based on their previous move direction.

Basically what I want to know is how do I jump and keep my previous momentum with only a little bit of air control?

private void HandleMovement()
    {
        float x = Input.GetAxisRaw("Horizontal");
        float z = Input.GetAxisRaw("Vertical");

        Vector3 input = new Vector3(x, 0, z).normalized;

        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        velocity.x = x * speed;
        velocity.z = z * speed;

        velocity.y += gravity * Time.deltaTime;

        moveVelocity = transform.TransformDirection(velocity.x, velocity.y, velocity.z);

        if (isGrounded)
        {
            controller.Move(moveVelocity * Time.deltaTime);

            if (Input.GetButtonDown("Jump"))
            {
                velocity.y += Mathf.Sqrt(jumpHeight * -2 * gravity);
            }

            if (velocity.y < 0)
            {
                velocity.y = -2;
            }
        }
        else
        {
            //Just for now. This needs to be replaced with something that allows directional velocity and limited air movement
            controller.Move(moveVelocity * Time.deltaTime);
        }
    }

Best Answer

  • MKdevMKdev Member
    Accepted Answer

    I guess there is no need of adding else as you want to move the player in both the condition (onground=true or false).

Answers

  • MKdevMKdev Member

    Do you want to move player while he is in air?

  • Definitely. Some air movement would be good.

  • Sorry if it says accepted answer, accidentally miss clicked and now I don't know how to reverse what I did.

    I tried without the else statement already but because of the way the script handles gravity, either I get frozen in mid air and I literally cannot move my character until I reset the game or I jump but without any "velocity" and it's more like I'm jumping in place

Sign In or Register to comment.