Cube flying instead of jumping,Cube starts to fly when holding jump key

Hello, Im trying to make a simple jump feature with my cube, except it starts to fly. Any help? here’s the code:

using UnityEngine;

public class PlayerMovement : MonoBehaviour {

    public Rigidbody rb;

    public float ForwardForce = 2000f;
    public float SidewaysForce = 500f;

	// Use this for initialization
	void Start () {
	}
	
	// Update is called once per frame
	void FixedUpdate () {
        rb.AddForce(0, 0, ForwardForce * Time.deltaTime);

        if (Input.GetKey("right"))
        {
            rb.AddForce(SidewaysForce * Time.deltaTime, 0, 0);
        }

        if (Input.GetKey("left"))
        {
            rb.AddForce(-SidewaysForce * Time.deltaTime, 0, 0);
        }
        
        if (Input.GetKey("up"))
        {
            rb.AddForce(0, 1000 * Time.deltaTime, 0);
        }
    }
}

change this

if (Input.GetKey("up"))
         {
             rb.AddForce(0, 1000 * Time.deltaTime, 0);
         }

to this

if (Input.GetKeyDown("up"))
         {
             rb.AddForce(0, 10 , 0);
         }

Most likely one of the following is happening:

  • Do you have gravity? If it has no gravity, it’s like jumping in space: you will fly with a simple tiny jump!

  • By using the GetKey, it will be adding the force every frame that you have the key down. GetKey returns true every frame the key is pressed. So if you keep pressing one of the keys, it will add the force every frame, making it fly away while you hold the key. You might want to use GetKeyDown instead.

  • Depending on what behaviour you are aiming for, you may want to use mode = ForceMode.Impulse in your AddForce call.