Character Spawn In Animation

Hi guys,

Im new to Godot and following along Brackeys tutorial as well as adding in some of my own stuff

One thing I cannot figure out is how to add in a spawn animation, so that when I start the game, it plays the spawn animation while the character stays in the same place, then goes into being able to move and jump with gravity being involved etc

Im currently using a _physics_process function for all my movement and animations, and AnimatedSprite2D to handle the animations

I have tried to use the autoplay on load feature for my spawn animation but when I start the game it just goes straight into the idle animation, and I have also tried running the _ready function and playing the animation from there but that also does not work

Any ideas? :slight_smile:

Here is the code:

extends CharacterBody2D

const SPEED = 200.0
const JUMP_VELOCITY = -300.0

# Get the gravity from the project settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)

@onready var animated_sprite = $AnimatedSprite2D

func _physics_process(delta):

# Add the gravity.
if not is_on_floor():
			velocity.y += 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.
var direction = Input.get_axis("move_left", "move_right")

# Flips sprite
if direction > 0:
	animated_sprite.flip_h = false
elif direction < 0:
	animated_sprite.flip_h = true

# Play Animation
if is_on_floor():
	if direction == 0:
		animated_sprite.play("Idle")
	else:
		animated_sprite.play("Run")
else:
	animated_sprite.play("Jump")
	if velocity.y > 0:
		animated_sprite.play("Fall")
	
# Applies movement
if direction:
	velocity.x = direction * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)

move_and_slide()
extends CharacterBody2D

const SPEED = 200.0
const JUMP_VELOCITY = -300.0
var is_spawned:bool=false

# Get the gravity from the project settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

@onready var animated_sprite = $AnimatedSprite2D

func _ready():
	animated_sprite.play("spawn")
	await animated_sprite.animation_finished
	is_spawned=true

func _physics_process(delta):
	if not is_spawned:
		return

	# Add the gravity.
	if not is_on_floor():
		velocity.y += 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.
	var direction = Input.get_axis("move_left", "move_right")

	# Flips sprite
	if direction > 0:
		animated_sprite.flip_h = false
	elif direction < 0:
		animated_sprite.flip_h = true

	# Play Animation
	if is_on_floor():
		if direction == 0:
			animated_sprite.play("Idle")
		else:
			animated_sprite.play("Run")
	else:
		animated_sprite.play("Jump")
		if velocity.y > 0:
			animated_sprite.play("Fall")
		
	# Applies movement
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()

Thank you! Works like a treat :slight_smile:

1 Like

So, I’m new as well, but wouldn’t it be easier to use set_physics_process(false) at the start to set physics process to false until the animation plays? Also, from all the awaits I’ve used(arguably not many but still), is there any need for anything else except for the await animated_sprite.animation_finished?

you will still need await for the animation to be finished, then you let the process started
there are many ways to do this, pick what you think is more suitable for you

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