Best way to implement 2D sprites with unique Left and Right sprites?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Onvochn

Hi, I’m fairly new to Godot, and I’ve been working on a game with a friend. She’s insistent on using sprites with unique animations, and I’m fine with it, but I’m fairly sure my current code isn’t the best way to do it. I’ve been looking at anim trees, or maybe doing a switch case, but would like some feedback on what I already have.

 func get_new_animation(is_shooting = false):
	var animation_new = ""
	if Input.is_action_pressed("game_right"):
		direction = Vector2.RIGHT
	elif Input.is_action_pressed("game_left"):
		direction = Vector2.LEFT
	if is_on_floor():
		if not is_shooting:
			if _velocity.x > 0.1:
				animation_new = "Right Run"
			elif _velocity.x < 0:
				animation_new = "Left Run"
			elif _velocity.x == 0:
				if direction == Vector2.LEFT:
					animation_new = "Idle Left"
				else:
					animation_new = "Idle Right"
		else:
			if _velocity.x > 0.1:
				animation_new = "Right Run Shoot"
			elif _velocity.x < 0:
				animation_new = "Left Run Shoot"
			elif _velocity.x == 0:
				if direction == Vector2.LEFT:
					animation_new = "Left Shoot"
				else:
					animation_new = "Right Shoot"
	else:
		if not is_shooting:
			if _velocity.y > 0:
				if direction == Vector2.LEFT:
					animation_new = "Left Fall"
				else:
					animation_new = "Right Fall"
			else:
				if direction == Vector2.LEFT:
					animation_new = "Left Jump"
				else:
					animation_new = "Right Jump"
		else:
			if direction == Vector2.LEFT:
				animation_new +=  "Left Jump Shoot"
			else:
				animation_new +=  "Right Jump Shoot"
	return animation_new

The function just checks which animations should play, and determines which side is facing by a Vector based on last input.
Thanks in advance!

:bust_in_silhouette: Reply From: Inces

I find it OK :slight_smile:
Of course if You want to spare this many lines of code You should use animation tree, but I don’t think it will help in performance so much.

I will give You a hint on what I do to reduce code for such cases. I name my animations meaningfully like “WalkShootLeft1” “JumpIdleIdle1” and construct animation name from components :

if abs(velocity.x) > 5  :
 anim = "Run" ( when slower its Walk, when vel.y is > 0 it is Jump )
if is pressed shoot :
  anim += "Shoot" , else anim += "Idle"
if velocity.x > 0 :
  anim += "Right", elif < 0 "Left" else "Idle"
return anim