Home > Mobile >  Can't Jump while moving left and right in 2D
Can't Jump while moving left and right in 2D

Time:10-18

A little bit of context, this is a 2D game I got it so I can move left right and jump, I can't jump while moving left and right and if I move while in midair then the player's decent dramatically slows down Here's the code:

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    [SerializeField] private float horSpeed;
    [SerializeField] private float vertSpeed;
    private Rigidbody2D rb;
    private bool isJumping;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        if (Input.GetKey(KeyCode.Space) && !isJumping)
            rb.velocity = new Vector2(0, horSpeed);
            isJumping = true;

        if (Input.GetKey(KeyCode.A))
            rb.velocity = new Vector2(-vertSpeed, 0);

        if(Input.GetKey(KeyCode.D))
            rb.velocity = new Vector2(vertSpeed, 0);

        if (rb.velocity.y==0f)
        {
            isJumping = false;
        }
    }

    
}

I don't see what I've done wrong here, it might be that I'm registering the inputs on different lines of code but I'm not sure how to fix it if that is the problem

CodePudding user response:

The reason that you can't jump while moving left or right is because you overwrite your "horizontal" speed when you move. Look at your code for moving left or right. Notice how you set your y velocity to 0 when a or d is pressed.

Try this:

private void Update() {

    if (Input.GetKey(KeyCode.Space) && !isJumping) {
        rb.velocity.y = horSpeed;
        isJumping = true;
    }

    if (Input.GetKey(KeyCode.A))
        rb.velocity.x = -vertSpeed;

    if(Input.GetKey(KeyCode.D))
        rb.velocity.x = vertSpeed;

    if (rb.velocity.y==0f)
    {
        isJumping = false;
    }

}

I think this should work but I don't have access to Unity right now so I'll have to wait til I get home to check it. Also, I didnt change it in this code but it seems like you have horSpeed and vertSpeed flipped. Up and down movement (a.k.a. jumping) should be vertSpeed.

CodePudding user response:

The code presented is dealing with the velocity of an object. As such, you want to assign the calculated velocity as one value once all factors have been taken into account. I’ve corrected a few points and come up with this:


private void Update()
{
    // we store the initial velocity, which is a struct.
    var v = rb.velocity;

    if (v.y==0f)
        isJumping = false;

    if (Input.GetKey(KeyCode.Space) && !isJumping) {
        v.y = vertSpeed;
        isJumping = true;
    }

    if (Input.GetKey(KeyCode.A))
        v.x = -horSpeed;

    if(Input.GetKey(KeyCode.D))
        v.x = horSpeed;

    rb.velocity = v;

}

The reason the code stores the velocity first, is because any changes you make to the velocity struct fields need to be applied back as a single velocity update (his is a difference between value and reference type objects).

  • Related