It should jump...But it is flying

Hi,I am realy new to c# and i need help.I make a very basic script that allows the player to jump.But the problem is it is flying not jumping.I want the player to jump a single time.If you can help i will be very happy :slight_smile:

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public Rigidbody rb;
    public float forwardForce = 2000f;
    public float jumpForce = 1000f;
    public float backForce = -2000f;
    




    void FixedUpdate()
    {

       

        if (Input.GetKey("w"))
        {

            rb.AddForce(0, jumpForce * Time.deltaTime, 0);


        }else if (Input.GetKey("d"))
        {


            rb.AddForce(0, 0, forwardForce * Time.deltaTime , ForceMode.VelocityChange);


        }else if (Input.GetKey("a"))
        {


            rb.AddForce(0, 0, backForce * Time.deltaTime , ForceMode.VelocityChange);


        }

       


    }
}

Input.GetKey() returns true every frame as long as the button is being held down. For discrete actions, you probably want to use Input.GetKeyDown() instead, which only returns true for one frame when the button is first pressed. (After that, it’s likely you’ll need to apply a lot more force than you did before.)

You might also want a rule that the player isn’t allowed to jump unless they were standing on the ground. (You can find lots of tutorials for this.) Though if you want the player to have infinite mid-air jumps then you don’t need to worry about that.

2 Likes