0 votes

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)
Godot version version 3.3.2
in Engine by (12 points)

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.

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.

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.