Problems trying to make idle animations

Godot Version

lates version 4.2.1

Question

I want to make the movement and the animation for the player, I said that I leave the movement at the end, first to make the animation work, I wrote the code and tried to make the idle animation exactly as the player was at the moment of the last movement. I specify that I made the animation in an AnimatedSprite2D named player_animation, and made several categories, for example player_animation.play(“idle_left”).
This is the code, that is, what’s left of it, I kept sitting and redoing, but in vain.

extends CharacterBody2D

@onready var player_animation = $player_animation

var direction = Vector2.ZERO
var last_position = Vector2.ZERO
var walk_speed = 8000
var run_speed = 15000

func _ready():
pass
func _process(delta):
if Input.is_action_pressed(“left”):
direction.x = -1
if Input.is_action_pressed(“shift”):
player_animation.play(“run_left”)
else:
player_animation.play(“walk_left”)

if Input.is_action_pressed("right"):
	direction.x = 1
	if Input.is_action_pressed("shift"):
		player_animation.play("run_right")
	else:
		player_animation.play("walk_right")

if Input.is_action_pressed("up"):
	direction.y = -1
	if Input.is_action_pressed("shift"):
		player_animation.play("run_up")
	else:
		player_animation.play("walk_up")

if Input.is_action_pressed("down"):
	direction.y = 1
	if Input.is_action_pressed("shift"):
		player_animation.play("run_down")
	else:
		player_animation.play("walk_down")

move_and_slide()

I try with last_position variable but I don’t know how to configure it

Reset direction at the start of every frame. Set last_position to direction at the end of every frame only if direction is not 0. If it is 0, use last_position to determine which idle animation to play.

Alternatively, you can use an AnimationTree, AnimationPlayer, and Sprite2D to cut down on code.

it work with this code, maybe help someone else
"
extends CharacterBody2D

@onready var player_animation = $player_animation

var direction = Vector2.ZERO
var last_direction = Vector2.ZERO

var walk_speed = 10000
var run_speed = 18000

var left
var right
var up
var down

func _ready():
player_animation.play(“idle_down”)

func _movement(delta):
left = Input.is_action_pressed(“left”)
right = Input.is_action_pressed(“right”)
up = Input.is_action_pressed(“up”)
down = Input.is_action_pressed(“down”)

direction = Input.get_vector("left", "right", "up", "down")
if Input.is_action_pressed("shift"):
	velocity = direction * run_speed * delta
else:
	velocity = direction * walk_speed * delta

if left:
	if Input.is_action_pressed("shift"):
		player_animation.play("run_left")
	else:
		player_animation.play("walk_left")
	last_direction = Vector2.LEFT
elif right:
	if Input.is_action_pressed("shift"):
		player_animation.play("run_right")
	else:
		player_animation.play("walk_right")
	last_direction = Vector2.RIGHT
elif up:
	if Input.is_action_pressed("shift"):
		player_animation.play("run_up")
	else:
		player_animation.play("walk_up")
	last_direction = Vector2.UP
elif down:
	if Input.is_action_pressed("shift"):
		player_animation.play("run_down")
	else:
		player_animation.play("walk_down")
	last_direction = Vector2.DOWN
else:
	if last_direction == Vector2.LEFT:
		player_animation.play("idle_left")
	elif last_direction == Vector2.RIGHT:
		player_animation.play("idle_right")
	elif last_direction == Vector2.UP:
		player_animation.play("idle_up")
	elif last_direction == Vector2.DOWN:
		player_animation.play("idle_down")

func _process(delta):
_movement(delta)
move_and_slide()
"

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