Problem with making the character in idle mode

version that I use godot 4.2

the character when it became idle, instead of being in the last directions it became in other directions

here’s my code

extends CharacterBody2D

@export var SPEED = 30

@onready var anime = $Gedeon/AnimationPlayer
	
var last_direction_x = 0
var last_direction_y = 0

	
func _physics_process(delta):
	
	var directionx = Input.get_axis("ui_left", "ui_right")
	if directionx != 0:
		velocity.x = directionx * SPEED
		last_direction_x = directionx
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
	var directiony = Input.get_axis("ui_up", "ui_down")
	if directiony != 0:
		velocity.y = directiony * SPEED
		last_direction_y = directiony
	else:
		velocity.y = move_toward(velocity.y, 0, SPEED)
	
	if directionx > 0.0:
		anime.play("walk_right")
	elif directionx < 0.0:
		anime.play("walk_left")
	elif directiony > 0.0:
		anime.play("walk_front")
	elif directiony < 0.0:
		anime.play("walk_back")
	
	if velocity.x == 0 and velocity.y == 0:
		if last_direction_y > 0.0:
			anime.play("idle")
		elif last_direction_x > 0.0:
			anime.play("right_idle")
		elif last_direction_y < 0.0:
			anime.play("back_idle")
		elif last_direction_x < 0.0:
			anime.play("left_idle")
		
	move_and_slide()

In Godot up is negative so it should be:

	if directionx > 0.0:
		anime.play("walk_right")
	elif directionx < 0.0:
		anime.play("walk_left")
	elif directiony < 0.0:
		anime.play("walk_front")
	elif directiony > 0.0:
		anime.play("walk_back")
	
	if velocity.x == 0 and velocity.y == 0:
		if last_direction_y < 0.0:
			anime.play("idle")
		elif last_direction_x > 0.0:
			anime.play("right_idle")
		elif last_direction_y > 0.0:
			anime.play("back_idle")
		elif last_direction_x < 0.0:
			anime.play("left_idle")

this is not what I meant

I meant when the character stops at let’s say right, it stops at front instead of right

because it hit the (front idle) argument first and didn’t execute the right idle argument

you can try this:

extends CharacterBody2D

@export var SPEED = 30

@onready var anime = $Gedeon/AnimationPlayer
	
var last_direction_x = 0
var last_direction_y = 0

	
func _physics_process(delta):
	
	var directionx = Input.get_axis("ui_left", "ui_right")
	if directionx != 0:
		velocity.x = directionx * SPEED
		last_direction_x = directionx
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
	var directiony = Input.get_axis("ui_up", "ui_down")
	if directiony != 0:
		velocity.y = directiony * SPEED
		last_direction_y = directiony
	else:
		velocity.y = move_toward(velocity.y, 0, SPEED)
	
	if velocity.x > 0.0:
		anime.play("walk_right")
	elif directionx < 0.0:
		anime.play("walk_left")
	elif directiony > 0.0:
		anime.play("walk_front")
	elif directiony < 0.0:
		anime.play("walk_back")
	
	if velocity.x == 0 and velocity.y == 0:
		if Input.is_action_just_released("ui_up"):
			anime.play("idle")
		if Input.is_action_just_released("ui_right"):
			anime.play("right_idle")
		if Input.is_action_just_released("ui_down"):
			anime.play("back_idle")
		if Input.is_action_just_released("ui_left"):
			anime.play("left_idle")
		
	move_and_slide()

I swear, you saved me, thank you so much bro

man, I’ll mention you in my dev log to appreciate your help

1 Like

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