How to make my player stay idle in the direction I stopped moving in.

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

So, Im pretty new to Godot and Im making a Top Down game like Zelda except I want my sprite to be able to look only to left or right. I have that setup but my problem is that when I walk left and stop the idle animation is facing right. Same if Im facing left and going up and release left my character switches back to right, like it prefers to face right when going up or down. I know I have to store my direction in a variable and then tell the game to face that stored direction or something similar. Im using godot for only like a month and need help with this. If you find a tutorial or a guide on how to do it please tell me. Or you can tell me yourself. If you need any more information like screenshots or code please let me know.

Thanks in advance :slight_smile:

:bust_in_silhouette: Reply From: Gluon

Well I dont know how you have written this at the moment but I assume you have an animated character set up? I will assume below that there is a character with four animations which i will call AniLeft, AniRight, AniUp, AniDown. Obviously you will need to change it to suit what you have named your animations.

Func process(delta):
var vel = vector2.zero
if input.is_action_pressed(“ui_left”):
vel.x += 1
if input.is_action_pressed(“ui_right”):
vel.x -= 1
if input.is_action_pressed(“ui_up”):
vel.y -= 1
if input.is_action_pressed(“ui_down”):
vel.y += 1
move_and_slide(vel.normalized() * 100)
personalAnimationControl(vel)

func personalAnimationControl(vel)
if vel.x > 1:
$“AnimatedSprite”.play(“AniRight”)
elif vel.x < 1:
$“AnimatedSprite”.play(“AniLeft”)
elif vel.y > 1:
$“AnimatedSprite”.play(“AniDown”)
elif vel.x < 1:
$“AnimatedSprite”.play(“AniUp”)
else:
$“AnimatedSprite”.stop
$“AnimatedSprite”.frame = 0

so the above shouldnt cause the problem you are seeing at least but I dont know how different that is from your code?

Hello,

Im sorry I didn’t put any more information. Im using an AnimationPlayer and my character has only one running animation and then I flip the sprite even though its an rpg. I like this kind of style. And my movement is setup with an input_vector like this:

func _physics_process(delta: float) -> void:
var input_vector = Vector2.ZERO
input_vector.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
input_vector.y = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
input_vector = input_vector.normalized()

(of course I do have the lines indented but it doesnt show here)

if input_vector != Vector2.ZERO:
    animPlayer.play("Walk")
else:
    animPlayer.play("Idle")

I know this is a little weird to do a top down game like this so sorry :slight_smile:

again if you need more information just tell me

xtuneon | 2021-08-17 20:16

Hey update:

Im dumb… My sprite flipping script had a If and else. but i changed else with elif and now it fixed every problem i had. Thanks for your help!

xtuneon | 2021-08-18 08:10