Hello. I'm currently struggling to properly animate my player character's sprite. When I hit play and move in any direction only my character's forward walking animations initialize: Including Idle Forward and Walk Forward. For context my character has a forward walking animation, a left; a right; and backward one.
Here is the code I put together:
extends CharacterBody2D
# initiates animation tree and speed
@onready var anim_tree = get_node("ClaymanAnimTree")
const SPEED = 85.0
# reads and interprets movement inputs
func get_input():
var input_direction = Vector2(
Input.get_vector("Move Left", "Move Right", "Move Up", "Move Down")).normalized()
velocity = input_direction * SPEED
if input_direction == Vector2.ZERO:
#stays on idle animation when character is still
anim_tree.get("parameters/playback").travel("Idle")
else:
#is supposed to enable the four-way movement in the sprite animation
anim_tree.get("parameters/playback").travel("Move")
anim_tree.set("parameters/Idle/Blendspace2D/blend_position", input_direction)
anim_tree.set("parameters/Move/Blendspace2D/blend_position", input_direction)
func _physics_process(delta: float) -> void:
#physics
get_input()
move_and_slide()
I am relatively new to programming in a game engine and engine forums, so do forgive me if I’ve left out any necessary context.
Is this how you store the last direction to use when idle? Like set to left when walking left, and then used if no input?
Try adding prints for the directions and double check what the blend_positions correspond to in the animation tree. Have you set up all the animations to match the input directions you send over here in else?
Hello baba. Thank you for your response.
To answer your first question, I indeed get the forward idle/walk for every direction.
For your second question, yes, that is how I am storing the last direction the character faces when idle and not moving.
I’ve taken some time to review your suggestions, and believe I’ve made progress. Knowing that the animations should match the input directions, I began to check again on the animation tree’s blending positions to realize that all of this time I had the coordinates in specific places via the pointer. After changing these coordinates, the animations then began to be stuck in those corresponding directions.
I think that now all I need to do is find out how to reset these coordinates entirely so that they are not read in certain directions. I’m assuming what I need to do is fix the blend position pointer somehow, or remove it? Here is a screenshot. I can’t seem to center it fully.
Im not sure if i understand your question. You want to make it so that parts of the blendspace is empty without any corresponding animations? So there is no up /down for example?
If that is not possible, and I appear to be missing an integral detail in my assumptions than I do apologize. I’m doing my best to learn the ins and outs of Godot as a beginner.
You could, i believe, delete the top and bottom nodes in your current blendspace. The remaining nodes along y=0 would form a line. Then you could use it for left/right.
Creating a Blendspace1d would also get a line you could use for left/right but that would require you to not send over the entire vector2 as input direction, but only the x component.
I believe that I did not articulate myself properly earlier on. Not that this is significant now, I’ve found out that either or the earlier approach that I’d attempted would not work.
I’m back to square one.
To restate my goal somewhat in accordance to what I’ve learned: My character’s sprite is always stuck in its two corresponding directional animations. I.E.: Walk Forward, Idle Forward.
I have tried checking the animation tree for what could have caused this issue, but to no avail. I can’t find any particular flaw that would make it so that the way I’ve structured tree itself is the issue. (If anyone can think of common examples that I perhaps didn’t keep in mind though, I’d appreciate the input)
This leads me to think that my script is imperfect. How I made it in the first place was through a tutorial online which utilized a dated version of Godot: 4.2.
If possible I’m wondering if there are other ways to structure my script in a way which will play the sprite animations properly. So far my project only has one script, and it is this one.