How to make an animation trigger upon landing

I am trying to make an animation play upon landing and it is not working and i am unsure of why.

if velocity.x == 0 and velocity.y < 0 and is_on_floor():"Fall")

It’s hard to say without any other information but I think you should change “velocity.y < 0” to “velocity.y >= 0”. Velocity.y < 0 (negative velocity) likely means that your character is going up.

I apologize for the lack of info, here is the rest of the code

extends CharacterBody2D

const SPEED = 300.0
const JUMP_VELOCITY = -400.0
@onready var animated_sprite = $Sprite2D/AnimationPlayer
@onready var jump_anim: AnimationPlayer = $Sprite2D/AnimationPlayer
var landing : bool

# 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):
	# Add the gravity.
	if not is_on_floor():
		velocity.y += gravity * delta

	# 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("MoveLeft", "MoveRight")
	if direction:
		velocity.x = direction * SPEED
		velocity.x = move_toward(velocity.x, 0, SPEED)
	if velocity.x > 0 and velocity.y == 0:
		$Sprite2D.flip_h = false"Run")
	if velocity.x < 0 and velocity.y == 0:"Run") 
		$Sprite2D.flip_h = true               
	if velocity.x == 0 and velocity.y >= 0 and is_on_floor()  :"Fall")
	if velocity.x == 0 and velocity.y == 0 :"Idle")
	if Input.is_action_just_pressed("Jump") and is_on_floor():"Jump") 
		velocity.y = JUMP_VELOCITY
	if velocity.y < 0 and is_on_floor():"Jump")
	# Replace with function body.   

After the change of velocity, my idle animation is now playing which i believe is stopping the landing animation from occurring

because is is_on_floor()
better way is create RayCast2D and check when is not is_on_floor() and raycast is_colliding, start playing landing animation, and when is_on_floor() start play idle

Thank you

After the changes the animation is now playing despite not being anywhere close to the ground, i tried changing the raycast direction to up just to check however the problem persisted. I think it might be colliding with the parent object, but i am unsure of why this would occure due to collid with parent being disabled

if velocity.x == 0 and velocity.y > 0 and $RayCast2D.is_colliding and !is_on_floor()  :"Fall")

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.