How to program the ability to jump in 4.0.2 (In 3d)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Entity2020 studios

I’m new to version 4.0.2 of Godot, and I tried to program the ability to jump in my 3d game, but it doesn’t work. How do I fix this?

:bust_in_silhouette: Reply From: ibrahim.semab

Sure, I can help you with that!

First, you’ll want to add a KinematicBody node as your player character. This will allow you to use the move_and_slide() function to move the character around in a physics-based way.

Next, you can add a RayCast node as a child of the player character. This will allow you to detect when the character is touching the ground and is therefore able to jump.

Finally, you can add some code to the _input() function of the player character to detect when the jump button is pressed and apply a vertical impulse to the character’s velocity.

Here’s some sample code that should get you started:

extends KinematicBody

const GRAVITY = -9.81
const JUMP_SPEED = 10
const FLOOR_NORMAL = Vector3(0, 1, 0)

var velocity = Vector3.ZERO
var is_grounded = false
var jump_count = 0

func _ready():

func _physics_process(delta):
    if is_grounded:
        velocity.y = 0

    velocity.y += GRAVITY * delta
    velocity = move_and_slide(velocity, FLOOR_NORMAL)

func _input(event):
    if event.is_action_pressed("jump") and jump_count < 2:
        velocity.y = JUMP_SPEED
        jump_count += 1

func _on_FloorDetector_body_entered(body):
    is_grounded = true
    jump_count = 0

func _on_FloorDetector_body_exited(body):
    is_grounded = false

In this code, we use the _physics_process() function to update the character’s velocity each frame. We start by checking whether the character is grounded, which we detect using a FloorDetector node (which is just a RayCast node pointed downwards from the character). If the character is grounded, we set their vertical velocity to zero so they don’t fall through the floor.

Next, we apply gravity to the character’s velocity, and then call move_and_slide() to move the character according to their current velocity and the floor normal (which should always be straight up).

In the _input() function, we detect when the jump button is pressed and apply a vertical impulse to the character’s velocity if they are grounded and haven’t already jumped twice in a row.

Finally, we use _on_FloorDetector_body_entered() and _on_FloorDetector_body_exited() to keep track of whether the character is grounded or not.

I’ll give this a go. The issue with falling through the floor has already been solved thanks to collision shapes.

Entity2020 studios | 2023-05-03 15:21

You can’t add arguements to move and slide in Godot 4. The variable has to be defined on it’s own.
And also Raycast doesn’t have a signal for bodies or areas entering in Godot 4. I’ll have to use an Area3d node as a subsitute for the Raycast.
Your code still works after applying those changes, but jumping causes the player to keep going upwards. That can be fixed by checking if the player is on the floor, and applying downwards force when they aren’t.
Doing this allows the player to go back down after jumping, but it prevents them from jumping again. I attempted to use parts of your code to fix this, but it didn’t work.

Entity2020 studios | 2023-05-03 15:37