tutorials/2d/2d_sprite_animation #54
Replies: 5 comments 5 replies
-
| Spritesheet animation example for four directions of animation with eight directions of movement, prioritizing front and back animations: extends CharacterBody2D
# Animation Player child node
@onready var animation_player = self.get_node("AnimationPlayer")
# Properties
@export var speed = 250.0
var face_direction = "Front"
var animation_to_play = "Front_Idle"
# Start front idle animation on load
func _ready():
	animation_player.stop()
	animation_player.play("Front_Idle")
func _physics_process(_delta):
	# Reset velocity
	velocity = Vector2.ZERO
	
	# Add appropriate velocities depending on button press
	if Input.is_action_pressed("ui_left"):
		velocity.x -= 1.0 * speed
		# Only face left/right if not diagonal movement
		if velocity.y == 0.0:
			face_direction = "Left"
	if Input.is_action_pressed("ui_right"):
		velocity.x += 1.0 * speed
		# Only face left/right if not diagonal movement
		if velocity.y == 0.0:
			face_direction = "Right"
	if Input.is_action_pressed("ui_up"):
		velocity.y -= 1.0 * speed
		face_direction = "Back"
	if Input.is_action_pressed("ui_down"):
		velocity.y += 1.0 * speed
		face_direction = "Front"
		
	# All movement animations named appropriately, eg "Left_Idle" or "Back_Walk"
	animation_to_play = face_direction + "_" + ("Walk" if velocity.length() > 0.0 else "Idle")
	animation_player.play(animation_to_play)
	
	# Move character, slide at collision
	move_and_slide() | 
Beta Was this translation helpful? Give feedback.
-
| When animating a spritesheet you don't have to manually add all frames and adjust the values. You can just add the first and last frame, set the values, and then adjust the animation from "Discrete" to "Continuous". (That's the leftmost icon on the right side of the track) | 
Beta Was this translation helpful? Give feedback.
-
| Hey, How would I flip one animation w/o flipping all of them? The sprite sheet I'm using only has running to the right, but I need the character to be able to go left too. | 
Beta Was this translation helpful? Give feedback.
-
| If you add a  | 
Beta Was this translation helpful? Give feedback.
-
| Would be nice if animationplayer could support drag and drop of sprites and/or sprite sheet for automatic keyframes | 
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
tutorials/2d/2d_sprite_animation
Introduction: In this tutorial, you'll learn how to create 2D animated characters with the AnimatedSprite2D class and the AnimationPlayer. Typically, when you create or download an animated charact...
https://docs.godotengine.org/en/stable/tutorials/2d/2d_sprite_animation.html
Beta Was this translation helpful? Give feedback.
All reactions