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)