My player isn't staying idle in the direction it stopped moving in

Godot 4

Hi! I’m trying to use stacked sprites in Godot 4 for the first time using this tutorial

and some additional code to animate the character, however i’m running into an issue where the character keeps reverting to facing left whenever i let go off the keys. I want to make it so that it faces the direction it was moving in. any help? I’ve attached the code im using to try and do so and a gif of the issue as well.

screen-recording(3)

You set the rotation of $StackedSprite twice during one run. maybe you have to remove the first one?

1 Like

tried it, didn’t work. if i take the first time i call the rotation out it still works the same, but if i take out the part i marked it stop sturning completely.

Keep the first one deleted, then print out the rotation of your stackedsprite in your if statement(after you set it) and again after all your methods ran. Check in the console if the values are correct throughout the course of the function

alright tried that
all the print stuff is showing null or blank statements nothing concrete i can use to see if the values are correct :sweat_smile:

i’ve noticed though when i remove all the code for animation it works perfectly fine, but when i put the animation code back in it does the weird left position thing, all the code for moving and animating the sprite attached below.

sorry for all the trouble, thanks for trying to help :melting_face:

It’s more helpful to paste the code. The </> button on the forum will make three ticks like so for formatting code

```
type or paste code here
```

You still have the first $StackedSprite.set_b_rotation(rotation) in this script. Might help to use if not input.is_zero_approx() instead of equivalence comparing vectors to zero.

Have you checked it the “idle” animation maybe has a key for the sprite-rotation?

1 Like
extends CharacterBody2D

@export var speed: int = 30
@export var idle_frames: Array[Texture] = []
@onready var animation = $AnimationPlayer

var input = Vector2.ZERO

func set_idle_animation(frame):
	$StackedSprite.texture = idle_frames[frame]


func updateAnimation():
	if velocity.length() == 0:
		animation.play("idle")
	else:
		animation.play("walk")

func handleInput():
	var moveDirection = Input.get_vector("left", "right", "up", "down")
	velocity = moveDirection * speed

func _physics_process(delta):
	$StackedSprite.render_sprites()
	#$StackedSprite.set_b_rotation(rotation)
	input.x = Input.get_action_strength("right") - Input.get_action_strength("left")
	input.y = Input.get_action_strength("down") - Input.get_action_strength("up")
	if input != Vector2.ZERO:
		$StackedSprite.set_b_rotation(input.angle() - deg_to_rad(180))
	handleInput()
	move_and_slide()
	updateAnimation()

here’s the code pasted! and may i ask where the if not input.is_zero_approx() goes exactly? sorry i’m a little new to coding in gdscript

1 Like
#if input != Vector2.ZERO:
if not input.is_zero_approx():

It’s the same comparison, but computers handle floats (decimal numbers) in a weird way that ends up making actual exact zero hard to hit.

Also might be easier/better to use input vector function, depends on how you want diagonal inputs to work. Try it out, the difference is subtle. get_vector makes diagonal movement the same speed as axis-aligned, your method makes diagonal movement about 30% faster.

input = Input.get_vector("left", "right", "up", "down")
if not input.is_zero_approx():

it just has no key for sprite rotation, just a key looping for the idle animation

good info to know for the future! but i tried this and nothing still the same
i think i might try moving things around a bit more and seeing what works what doesn’t. i had turned to the forum in despair lol, but maybe i might email the original person who gave me the animation code and see if he can figure it out too.

thanks for helping out!! sorry nothing worked out :melting_face:

Do you have a RESET-Track?

nope, i don’t

update: figured it out, had to keep moving the code around and an extra line here and there and it worked :+1: thank for all the help!