Player animation best practice

Hey all!

I’m complete new to game development so this question is more about best practices for handling movement input and animations together for a player character. There’s going to be a fair but of text but ultimately the question is:

Is this a good way of doing it?

I’m currently following a Udemy course where the course teacher handles all movement and animations in the “player” _physics_process function by creating a boolean variable to detect each movement type, calling them to change the velocity, calling move_and_slide and then calling them again to change the animations.This just felt a little bit messy to me so I rewrote it to the below.

The player has some jump start and double jump animations which makes this a bit more confusing but ultimately I’ve tried to just make it more compact at the expensive of probably making it more messy.

However, I’ve read that this is bad practice because the animation is started before the movement is called so it can end up a frame ahead. Is this true?

Is it better to handle movement before move and slide and then animations after? At the cost of repeating code?

Bonus question:
The only way I could find to run one animation into the next was the call the 2nd when the animation_finished signal was emitted. This also seems a bit messy. Is there a better way of doing this?

extends CharacterBody2D
@export var movement_speed = 300
@export var jump_strength = 450
@export var max_jumps = 2
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var jump_count = 0
@onready var player = $AnimatedSprite2D

func _ready():
func _physics_process(delta):
	if not is_on_floor():
		velocity.y += gravity * delta
	var direction = Input.get_axis("move_left", "move_right")
	velocity.x = direction * movement_speed
	if is_on_floor():
		if !direction:"idle")
		jump_count = 0
		if Input.is_action_just_pressed("jump"):
			jump_count += 1
			velocity.y = -jump_strength"jump_start")
		if velocity.y > 0:"fall")
			if Input.is_action_just_pressed("jump") and jump_count < max_jumps:
				jump_count += 1
				velocity.y = -jump_strength"double_jump_start")
		elif velocity.y < 0 and Input.is_action_just_released("jump"):
			velocity.y = 0
	if direction < 0:
		player.flip_h = true
	elif direction > 0:
		player.flip_h = false
func _on_jump_finished():"jump")

No, It is best to start animation before movement, there is no difference on it and about your bonus question, it is ok but if you use animation tree, then it will be better. Animation tree is best for smooth animation transition


So the way I’m doing it in my code is fine? (if a little messy)

1 Like

Yes, its fine