Animation conflicts in script

Godot Version

4.3

Question

Hi, I’m a new learner on Godot and coding. I’m putting together a little jetpack game just as an exercise to learn all the basics in movements animation etc.
I have a working player script, everything does what it’s supposed to. I’m trying to add one more small detail when the player is free falling I want to play a different animation (in this case only one sprite) It’s commented out at the bottom before move and slide().
I’ve tried a number of ways to do it but it always cancels my death animation “explode”. The physics process function has an alive check at the start and I tested with print() and it all works but when I add the falling code the death animation only shows the first sprite and doesn’t complete it’s animation before the scene is reset. I’ve been a few days trying different things out but I don’t have the experience to figure out why its conflicting. Any help highly appreciated as always!

Here´s my code



extends CharacterBody2D
const SPEED = 450.0
var alive : bool = true
var ACCELERATION = 40
var JUMP_VELOCITY = -700
var DESCEND_VELOCITY = 1300

@onready var proto: AnimatedSprite2D = $proto


func _ready() -> void:
	add_to_group("player")


func _physics_process(delta: float) -> void:
	
	if !alive:
		
		return
		
	velocity += Vector2() * delta
	
	if is_on_floor() and velocity.x == 0:
		proto.animation = "idle"
		velocity.y = 0
	
	# Get the input direction and handle the movement/deceleration.
	var direction = Input.get_axis("ui_left", "ui_right")
	if direction and is_on_floor():
		proto.animation = "walk"
		velocity.x = direction * SPEED
		
	elif direction and !is_on_floor():
			velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, 30)
		
	if velocity.x < 0:
		proto.flip_h = true
	if velocity.x > 0:
		proto.flip_h = false
		
		
		# Handle jetpack.
	if Input.is_action_pressed("ui_accept"): 
		proto.animation = "jetpack2"
		velocity.y -= ACCELERATION
		velocity.y = clamp(velocity.y, JUMP_VELOCITY, -JUMP_VELOCITY)
	else:
		velocity.y += ACCELERATION 
		velocity.y = clamp(velocity.y, JUMP_VELOCITY, DESCEND_VELOCITY)
	
	#if !is_on_floor() and Input.is_action_just_released("ui_accept"):
		#proto.animation = "falling"
	move_and_slide()
	
func _on_killzone_player_died():
	alive = false
	print ("dead")
	proto.animation = "explode"

Try await some physic frames before set the animation, maybe you’re setting the animation while the _physics_process code is already executing:

func _on_killzone_player_died():
	alive = false
	print ("dead")

	# Await two physics frame
	await get_tree().physics_frame
	await get_tree().physics_frame

	proto.animation = "explode"

Thanks! The idea did cross my mind. Wasn’t sure if it was really good practice or not.
I’m (trying to)setting up a state machine see if I can learn the correct way to manage these movements. :blush::pray:t4:

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