Can't get the way the play is facing to save when picking a new animation.

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

I’m new to GDscript and the Godot engine and have been trying to get the animations of my character to work like I want them to for a while now. Whenever I move my character it doesn’t save the way the character was facing the last time it was moved and reverts to the animation that I set to “play on load”. btw I’m using an animationtree for this. Any help would be appreciated.

Code:

extends KinematicBody2D

var movespeed = 150


#Character Movement and animations
 func _physics_process(delta):
var velocity = Vector2()
if Input.is_action_pressed("ui_left"):
	velocity.x -= 4

if Input.is_action_pressed("ui_right"):
	velocity.x = 4

if Input.is_action_pressed("ui_up"):
	velocity.y -= 4
if Input.is_action_pressed("ui_down"):
	velocity.y = 4
velocity = velocity.normalized()

if velocity == Vector2.ZERO:
	$AnimationTree.get("parameters/playback").travel("IdleLeft")
elif velocity.x < 0:
	$AnimationTree.get("parameters/playback").travel("RunLeft")
	$AnimationTree.set("parameters/RunLeft/position", velocity)
elif velocity.x > 0:
	$AnimationTree.get("parameters/playback").travel("RunRight")
	$AnimationTree.set("parameters/RunRight/position", velocity)
elif velocity.y < 0:
	$AnimationTree.get("parameters/playback").travel("RunLeft")
	$AnimationTree.set("parameters/RunLeft/position", velocity)
elif velocity.y > 0:
	$AnimationTree.get("parameters/playback").travel("RunRight")
	$AnimationTree.set("parameters/RunRight/position", velocity)
else:
	$AnimationTree.set("parameters/IdleLeft/position", velocity)
	
	
	



velocity = move_and_slide(velocity * movespeed)

I’m sorry I can’t help much but you posted the question twice:

You can notice first link has ID 113447 and second link has ID 113525.
You might want to only keep one open.

a0kami | 2021-08-17 21:12

You are setting the animation to "IdleLeft" when there is no velocity, so it will always face left when the character stops moving. You need to have idle textures for all the directions your sprite can go, and then set to the correct one regarding the last animation it was playing.

For example, you can use Input.is_action_just_released to get the moment when the player just stopped pressing a key, and set the correct animation there.

Ev1lbl0w | 2021-08-26 10:09