0 votes

I'm trying to assign different animations to movements of a Player character. However, I'm facing some sort of issue in my code: writting "else: _animated sprite.stop()" to every individual line appears to cancel the animations all together, whilst writting it in single line allows only one of the animations to play.

extends KinematicBody2D

export (int) var speed = 200

onready var animatedsprite = $AnimatedSprite

var velocity = Vector2()

func get_input():
velocity = Vector2()

if Input.is_action_pressed("ui_right"):
    velocity.x += 1
    _animated_sprite.play("Right")
else:
    _animated_sprite.stop()

if Input.is_action_pressed("ui_left"):
    velocity.x -= 1
    _animated_sprite.play("Left")
else:
    _animated_sprite.stop()

if Input.is_action_pressed("ui_down"):
    velocity.y += 1
    _animated_sprite.play("Forward")
else:
    _animated_sprite.stop()

if Input.is_action_pressed("ui_up"):
    velocity.y -= 1
velocity = velocity.normalized() * speed
Godot version 3.5.1
in Engine by (15 points)

1 Answer

+1 vote
Best answer

You should consider using an AnimationTree instead.
With the BlendSpace2D Godot itself will change the animation based on your input.

https://docs.godotengine.org/en/stable/tutorials/animation/animation_tree.html#blendspace2d

This part in the tutorial specifically shows how to use the BlendSpace2D.
And this youtube video should show the steps to get an AnimationTree to do the heavy lifting in a 2D game: https://www.youtube.com/watch?v=Xf2RduncoNU

Good luck with your project!

by (189 points)
selected by

Thank you for the help!

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 Frequently asked questions and 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 [email protected] with your username.