Ground abandonment timer

Godot Version

4.2.1

Question

Hi everyone, I want to make a timer when a player leaves a platform in a platformer, but I can’t figure out how (for example he leaves the platform but reacts too late and falls, and with a timer he would have a few milliseconds to react). I’ll attach the code, but it’s standard Godot code.

extends CharacterBody2D

@onready var sprite_2d = $Sprite2D

const SPEED = 500.0
const JUMP_VELOCITY = -900.0

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")


func _physics_process(delta):
	#Animations
	if(velocity.x > 1 || velocity.x <-1):
		sprite_2d.animation = "running"
	else:
		sprite_2d.animation = "default"
	
	# Add the gravity.
	if not is_on_floor():
		velocity.y += gravity * delta
		sprite_2d.animation = "jumping"

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var direction = Input.get_axis("Left", "Right")
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, 29)

	move_and_slide()
	
	var isLeft = velocity.x < 0
	sprite_2d.flip_h = isLeft

A easy way is to add a timer, connect the timeout, in that function, do like that “time_spent += 1” means the time of player stay on the platform, so now add a area2d in the platform, connect area exited, write that timer.stop() and now you will get the time_spent_amount

1 Like