*****HELP! My jump code turned into a JETPACK!*****

godot version:
----------4----------

--------- Question:----------
so i have been using Godot to make a game for school which is 3rd person (3d)
i have the movement controls all sorted out using custom inputs to use the WASD keys to control the player and i have figured out the camera from a few youtube videos, however, i cant find a piece of code that makes the character jump that suits the current code i have. the piece of code that i wrote myself works, but has many problems, for example, if you hold down the spacebar you just keep ascending into the air infinitely and you can use the jump like a jetpack and jumping mid air. i also want to make the effects of gravity stronger so that i fall faster, instead of just floating down. I’m sure there is an easy way to change this by changing some value in the script, but I have no idea where.

just to clarify, the Input “jump” is part of the custom input map.

here is my script:

extends RigidBody3D
#below shows code that values will be held with decimals
var mouse_sensitivity := 0.001
var twist_input := 0.0
var pitch_input := 0.0

@onready var twist_pivot := $TwistPivot
@onready var pitch_pivot := $TwistPivot/PitchPivot

func _ready() -> void:
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
	
	
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	var input := Vector3.ZERO
	input.x = Input.get_axis("move_left", "move_right")
	input.z = Input.get_axis("move_forward", "move_back")
	
	apply_central_force(twist_pivot.basis * input * 1200.0 * delta)
	
	if Input.is_action_just_pressed("ui_cancel"):
		Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
		
	twist_pivot.rotate_y(twist_input)
	pitch_pivot.rotate_x(pitch_input)
	pitch_pivot.rotation.x = clamp(
		pitch_pivot.rotation.x,
		deg_to_rad(-90),
		deg_to_rad(50)
	)
	twist_input = 0.0
	pitch_input = 0.0

func _unhandled_input(event: InputEvent) -> void:
	if event is InputEventMouseMotion:
		if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
			twist_input = - event.relative.x * mouse_sensitivity
			pitch_input = - event.relative.y * mouse_sensitivity
	
	var velocity := Vector3(0, -25.0, 0)
	
	const SPEED := 20.0
	const GRAVITY := -50.0

	var input = Input.get_action_strength("jump")
	apply_central_force(input * Vector3.UP * 100.0)

I’d recommend making a check for if the player’s object is currently on the ground. This theoretically prevents the issue of the player having a “jetpack”.

I mostly work with 2d but I’ll try to point out some of the possible problems you may have and possible solutions.

  • You are adding a constant force every frame as long as the jump button is held.

The way this is written, it will continue to add a central force to every frame as long as the action strength is not ZERO. Somewhere before this code, you need to check if the jump action has just been pressed so it can know when to apply the input strength. Input.is_action_just_pressed(“jump”) should help you with this.

  • I think applying a one-time impulse will work better for the jump

Instead of adding a central force and having to deal with gravity and stuff, why not just use an impulse?

if Input.is_action_just_pressed("jump"):
     var input = Input.get_action_strength("jump")
	  apply_impulse(input * Vector3.UP * 100.0)
  • Are you sure you should be using a rigid body?

Seeing constants and variables like speed, velocity and gravity makes me think you adapted this code from a ChracterBody instead of a RigidBody. I don’t see where you apply those variables and constants to the rigid body.

thanks so much!! iv’e met with a friend who knows C# and we worked together to understand all the code you have helped me with and it all seems to be working now. and yes, we changes it from a rigid body to a characterbody. thanks again!