Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my character's horizontal velocity lost when jumping?

GOAL
I'm relatively new to Unity and I want my character to be able to run and jump at the same time causing the character to go up diagonally.

PROBLEM
However after making some adjustments to give the character some acceleration, the jump seems to clear all existing velocity, meaning the character goes up and then to the side instead of both at the same time:

Video of issue

(I apologise if its a bit hard to see)

CODE
This is my character movement script:

    Rigidbody2D rb;
    BoxCollider2D bc;   

    [Header("Run")]
    float xInput = 0f;
    public float maxRunSpeed;
    public float acceleration;
    [Space]

    [Header("Jump")]
    public float jumpHeight;
    public float lowJumpHeight;
    public float fallSpeed;
    public float airControl;

    [Space]
    public LayerMask groundLayer;
    public bool onGround;

    [Space]
    public Vector2 bottomOffset;
    public Vector2 boxSize;
    public float coyoteTime;

    void Start() {
        // Gets a reference to the components attatched to the player
        rb = GetComponent<Rigidbody2D>();
        bc = GetComponent<BoxCollider2D>();
    }

    void Update() {
        Jump();

        // Takes input for running and returns a value from 1 (right) to -1 (left)
        xInput =  Math.Sign(Input.GetAxisRaw("Horizontal"));
    }

    // Applies a velocity scaled by runSpeed to the player depending on the direction of the input
    // Increaces the velocity by accerleration until the max velocity is reached
    void FixedUpdate() {
        rb.velocity = Math.Abs(rb.velocity.x) < Math.Abs(xInput) * maxRunSpeed ? rb.velocity + new Vector2(acceleration * xInput, rb.velocity.y) * Time.deltaTime : new Vector2(xInput * maxRunSpeed, rb.velocity.y);   
    }

    void Jump() {
        // Checks whether the player is on the ground and if it is, replenishes coyote time, but if not, it starts to tick it down
        coyoteTime = onGround ?  0.1f :  coyoteTime - Time.deltaTime;
        // Draws a box to check whether the player is touching objects on the ground layer
        onGround = Physics2D.OverlapBox((Vector2)transform.position + bottomOffset, boxSize, 0f, groundLayer);

        // Adds an upwards velocity to player when there is still valid coyote time and the jump button is pressed
        if (Input.GetButtonDown("Jump") && coyoteTime > 0) {
            rb.velocity = Vector2.up * jumpHeight;
        }
        
        // Increases gravity of player when falling down or when the jump button is let go mid-jump
        if (rb.velocity.y < 0 ) {
            rb.velocity += Vector2.up * Physics2D.gravity.y * (fallSpeed - 1) * Time.deltaTime;
        } else if (rb.velocity.y > 0 && !Input.GetButton("Jump")) {
            rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpHeight - 1) * Time.deltaTime;
        }
    }

Sorry for there being a lot of unecessary code, it's just im not sure what's causing the issue so i don't want to remove anything. Hopefully my comments make some sense?

like image 376
LWB Avatar asked Oct 24 '25 03:10

LWB


1 Answers

This is happening because you're setting the velocity of your rigidbody directly with rb.velocity = Vector2.up * jumpHeight. So that will wipe all existing velocity.

If you want to just add a force to the velocity rather than replacing it entirely, you can do that with methods like Rigidbody2D.AddForce.

like image 122
Nathan Wiles Avatar answered Oct 25 '25 17:10

Nathan Wiles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!