Godot Version
4.4.1
Details
My code use to work and the jump would be perfectly fine however when I went to reuse it in a new project the character won’t fall back down. The only difference that it’s in 3D.
extends CharacterBody3D
const speed = 5
@export var acc = 0.7
@export var dacc = 0.4
@export var jump_height : float
@export var jump_time_to_peak : float
@export var jump_time_to_descent : float
# float variables are variables that can interact with othe objects
@onready var jump_velocity : float = ((2 * jump_height) / jump_time_to_peak)
@onready var jump_gravity : float = ((-2 * jump_height) / (jump_time_to_peak * jump_time_to_peak))
@onready var fall_gravity : float = ((-2 * jump_height) / (jump_time_to_descent * jump_time_to_descent))
@onready var jump_buffer_timer = $jump_buffer_timer
@onready var coyote_timer = $coyote_timer
var buffered_jump = false
var is_jumping = false
var last_dir = 0
func _physics_process(delta):
if !is_on_floor():
velocity.y += GetGravity()*delta
var input_dir_x: float = Input.get_axis("left", "right")
var input_dir_z: float = Input.get_axis("forward", "backward")
# Do not normalize input
#movement
if input_dir_x != 0.0:
velocity.x = move_toward(velocity.x, speed * input_dir_x, acc) #moves the velocity towards speed*direction(-1 or 1) by the accelaration*delta
else:
velocity.x = move_toward(velocity.x, 0.0, dacc)#slows the player down towards 0 by the deacceleration*delta, removing delta breaks gravity
if input_dir_z != 0.0:
velocity.z = move_toward(velocity.z, speed * input_dir_z, acc) #moves the velocity towards speed*direction(-1 or 1) by the accelaration*delta
else:
velocity.z = move_toward(velocity.z, 0.0, dacc)#slows the player down towards 0 by the deacceleration*delta, removing delta breaks gravity
if Input.is_action_just_pressed("jump"):
buffered_jump = true
jump_buffer_timer.start() #jumps when you reach the floor if you clicked jump before touching the floor by starting a timer that if you land within it will jump
if Input.is_action_just_pressed("jump") or buffered_jump == true: #jumps if space is pressed or space was pressed early
jump()
if Input.is_action_just_released("jump") and velocity.y > 0:
velocity.y = velocity.y * 0.2 #if you let go of jump early you jump lower does this by multiplying the y velocity by 0.2 when space is released if the character is moving upwards
var was_on_floor = is_on_floor()#if the character was on the floor before checking
move_and_slide()
if was_on_floor and !is_on_floor():
coyote_timer.start() #starts the coyote timer if the player has moved from being on the floor to off the floor and if the player jumps within the timespan of the timer it will jump even though player is no longer on the floor
print(velocity.y)
func GetGravity() -> float: #determines whether the character should be falling or jumping by checking if the character is going upwards or not
if velocity.y > 0.0:
return jump_gravity
if is_on_ceiling() == true:
return fall_gravity
else:
return fall_gravity
func jump(): #if jump is pressed and the character is on the floor the y velocity equals the velocity of the jump
if is_on_floor() or !coyote_timer.is_stopped() and !velocity.y > 0:
velocity.y = jump_velocity
func _on_jump_buffer_timer_timeout() -> void:
buffered_jump = false