can't jump while moving

It just doesn’t jump while moving (well it does but its so tiny) someone please help, I’m completely new to coding plus I’m super stupid so if someone could help it would be super appreciated

Here is my code if it helps.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{

    private bool jumpkeywaspressed;
    private bool forwardwaspressed;
    private bool backwardswaspressed;
    private Rigidbody2D rb2d;
    float force = 40f;
    float speed = 2f;
    private bool isgrounded;

    // Start is called before the first frame update
    void Start()
    {
        rb2d = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {

        if (Input.GetKey(KeyCode.D))
        {
            forwardwaspressed = true;
        }
       
        if (Input.GetKey(KeyCode.A))
        {
            backwardswaspressed = true;
        }

        if (Input.GetKeyDown(KeyCode.W))
        {
            jumpkeywaspressed = true;
        }

    }

   //fixed update is called once every physics update
    private void FixedUpdate()
    {

       

        //check if A key was pressed down
        if (backwardswaspressed == true)
        {
            rb2d.velocity = Vector2.left * speed;
            transform.localRotation = Quaternion.Euler(0, 180, 0);
            backwardswaspressed = false;
            Debug.Log("A Key was pressed");
        }
       
        // check if D key is pressed down
        if (forwardwaspressed == true)
        {
            rb2d.velocity = Vector2.right * speed;
            transform.localRotation = Quaternion.Euler(0, 0, 0);
            forwardwaspressed = false;
            Debug.Log("D Key was pressed");
        }

        if (!isgrounded)
        {
            return;
        }

        //check if W key is pressed down
        if (jumpkeywaspressed == true)

        {
            rb2d.AddForce(Vector2.up *force, ForceMode2D.Impulse);
            jumpkeywaspressed = false;
            Debug.Log("W Key was pressed down");

           
        }

      


    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        isgrounded = true;
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        isgrounded = false;
    }

}

Lines 53 and 62 both wipe out the Y component of velocity, which is why you see the micro jump.

I assume you only intend to set the X component of velocity based on input.

To do that, replace line 53 above with something like:

Vector2 tempVelocity = rb2d.velocity;

tempVelocity.x = -speed;

rb2d.velocity = tempVelocity;

Same with line 62, except don’t negate speed.

If you want a more robust 2D controller, I put this one in my free proximity buttons project:

proximity_buttons is presently hosted at these locations:

https://bitbucket.org/kurtdekker/proximity_buttons

https://github.com/kurtdekker/proximity_buttons

Honestly mate you are a star you have helped me so much love you for it

1 Like