I have a landing animation that's supposed to play if the player hits the ground without doing anything otherwise it gets cancelled entirely and they do whatever input is active. Is there a way to check the moment the player collides with the ground and plays that animation? And how would I stop that animation and any point if the player decides to do something?

I'm a beginner but my code works for detecting the ground. This may help. Mainly the "is_on_floor" part of it.

    extends KinematicBody2D
    
    const UP = Vector2(0, -1)
    var motion = Vector2()
    export var speed = 2
    export var gravity = -2
    export var jump_force = -6.5
    onready var _animated_sprite = $AnimatedSprite
    
    func _physics_process(delta):
    	motion.y += gravity
    	if Input.is_action_pressed("ui_right"):
    		motion.x = speed / delta
    		if is_on_floor():
    			_animated_sprite.play("Walk")
    		_animated_sprite.flip_h = true
    	elif Input.is_action_pressed("ui_left"):
    		motion.x = -speed / delta
    		if is_on_floor():
    			_animated_sprite.play("Walk")
    		_animated_sprite.flip_h = false
    	elif Input.is_action_pressed("Block"):
    		if is_on_floor():
    			_animated_sprite.play("Blocking")
    			motion.x = 0
    	elif Input.is_action_just_released("Block"):
    		_animated_sprite.play("Block_release")
    		motion.x = 0	
    	else:
    		motion.x = 0
    		_animated_sprite.play("Idle")
    	if is_on_floor():
    		if Input.is_action_just_pressed("ui_up"):
    			motion.y = jump_force / delta
    			_animated_sprite.stop()	
    	motion = move_and_slide(motion,UP)

Already have something similar to this. I'm mainly looking to find a way to check the moment the player collides with the ground from a fall and play a one-shot animation from there if no other input is received.

This is my jump and fall code:

#Jump
	if is_on_floor() and Input.is_action_pressed("jump"):
		movement.y = -jumpSpeed;
		$AnimationPlayer.play("Jump");
		
#Fall
	if movement.y >= 0 and !is_on_floor():
		$AnimationPlayer.play("Fall");

You probably want to have a boolean you set when you are in the air, and then you can detect if that boolean is true when hitting the ground to detect if you just hit the ground. Something like this:

if is_on_floor():
	if (is_in_air == true):
		is_in_air = false
		$AnimationPlayer.play("Ground_Collide")
else:
	is_in_air = true

If you want to make sure the animation plays, I'd recommend having a boolean that "locks" the animation until it is finished. You can use the animation_finished signal to unlock the boolean once the animation is done. I would use a couple functions for this:

# the variable for locking the animation
var animation_lock = false

# connect the animation_finished signal in _ready
func _ready():
	$AnimationPlayer.connect("animation_finished", self, "on_animation_finished")

func play_animation(animation_name, add_lock_animation=false):
	if (animation_lock == true):
		return
	$AnimationPlayer.play(animation_name)
	animation_lock = add_lock_animation

func on_animation_finished(_animation_name):
	animation_lock = false

Then instead of calling AnimationPlayer.play in your player code, you'll instead want to call play_animation, optionally passing the second argument as true when you want to lock the animation so it can play fully. :smile:

I tried the first bit of code and I noticed the animation will play for about a split second before going back to idle.

I'm not entirely sure how to use the second code block (I'm really not much of a programmer, sorry. I have a really hard time understanding more advanced concepts), but assuming I'm understanding it right, I don't necessarily want to "lock" the player in the animation, I just want it to play and then go to the idle animation if no input is received when the player collides with the ground, otherwise the animation should just be canceled early or skipped entirely (the only thing that should consistently play in most cases is a landing particle effect, but that's a question for another time).

It seems like the first code block was on the right track, I just need it to actually play through the animation as long as the player doesn't do anything.

6 days later

@TwistedTwigleg That's just what I'm thinking. :smile:

a year later