How do i make that the game detects when the player jumps or falls

Godot Version

4.3

Question

i wanted to code that when the player jumps the animation “jump” plays and when he comes mid-air the animation “fall” starts but if the player just started falling without jumping that the animation “fall” will start and not “jump” but i dont know how should i code the jump or fall detector btw here is my code:

extends CharacterBody2D

const SPEED : int = 300
const JUMP_VELOCITY : int = -400

@onready var anim : AnimatedSprite2D = $AnimatedSprite2D

func _physics_process(delta: float) → void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta

# Handle jump.
if Input.is_action_just_pressed("jump") 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 Input.is_action_just_pressed("left"):
	anim.flip_h = true
if Input.is_action_just_pressed("right"):
	anim.flip_h = false
	
if is_on_floor():
	if direction > 0 or direction < 0:
		anim.play("run")
	if direction == 0:
		anim.play("idle")
else:
	anim.play("jump")

if direction:
	velocity.x = direction * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)

move_and_slide()

if you know what i have to code pls reply.I will appreciate it

Use the velocity.y, if you’re not on floor and velocity.y < 0 = you’re climbing, if you’re not on floor and velocity.y > 0 = you’re falling.

this is the updated code at the top of physics_process:

if not is_on_floor:
velocity += get_gravity() * delta

 if velocity.y < 0:
      anim.play("jump)
 if velocity.y > 0:
      anim.play("fall")

when i jump or fall only the jump animation plays and i typed the animation names correctly and the animations doesn’t have the same sprite. Can you explain me why it didnt work?

extends CharacterBody2D

const SPEED : int = 300
const JUMP_VELOCITY : int = -400

@onready var anim : AnimatedSprite2D = $AnimatedSprite2D

func _physics_process(delta: float) → void:
# Add the gravity.
if not is_on_floor():
	velocity += get_gravity() * delta
	# You added your new code here  <-------------
	 if velocity.y < 0:
		anim.play("jump)
 	if velocity.y > 0:
		anim.play("fall")

# Handle jump.
if Input.is_action_just_pressed("jump") 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 Input.is_action_just_pressed("left"):
	anim.flip_h = true
if Input.is_action_just_pressed("right"):
	anim.flip_h = false
	
if is_on_floor():
	if direction > 0 or direction < 0:
		anim.play("run")
	if direction == 0:
		anim.play("idle")
else:
	# But he's being overwrited here, the code you added
	# should be here instead <------------------------------
	anim.play("jump")

if direction:
	velocity.x = direction * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)

move_and_slide()
1 Like

thank you! i really appreciate it! also i puted the gravity there and deleted the if not is on floor at the top for the code to be optimized.It’s the second time you gave me a solution for my problems!I appreciate it! btw are you a moderator and do you know how i should learn gdscript from the right way because of the limited tutorials?

Nop, just a hobbyst programmer.


Read the “Getting Started” and “Manual” tabs from official Godot docs, you’ll learn not only GDScript but also how to use the entire engine.

thank you! now if i dont know something i will search it there and if i dont know what this code means i will go to the search help tab in the engine