I'm currently working on my own self imposed 2D platformer project prototype (link to the project at the bottom of this post) where I now have reached a point where I want to add animations to tweak the game's variables better.
For this project, I've largely based my project on the "Code a 2D Platform Game Character with Godot (Indie)", in this project I have a lot of the States that are in the early parts of the project: Move, Idle, Run, Air etc.
I feel like that I now have a reached a point, where if I want to tweak the variables better, then I want animations for the character.
What I have
To use the animations, I started watching GDQuest's youtube video: "2D Movement in Godot in Only 4 minutes" & in my "Player.tscn" scene, I have a Node2D scene called "PlayerSkin.tscn" that contains my sprite (which uses a spritesheet) & an animation player node where my character is animated.
At the moment, I don't really know how I should start triggering the animations. To me, it would make sense to keep the references to the "PlayerSkin" (the Node2D scene & the animation player) inside the "Player.gd" script & trigger the animations depending on the different states that the character enter & exit.
I need some advice how I should approach this & would appreciate even more some examples/suggestions on how I should do this.
Thank you for your time!
Link to Project Removed
I think I solved it.
Now I create a function to use it, for example:
This is A solution, I won't mark this post as solved in case someone suggests a solution that's either more efficient, reliable or just better overall.
Hey FunctionSam! This looks like a solid solution you have built for yourself. As the needs of your game expand let us know if you need help adapting it, but for now I feel like this should get your player animating correctly and I have used similar types of solutions in some projects of my own. Thanks for posting your solution it's really helpful!
Thanks you for the encouragement, I appreciate it a lot!
I have a question, should I trigger the animations from the func _physics_process(delta: float) -> void: or from the func _process(delta: float) -> void:?
When I tested with the _process function the animations, I think the animations didn't really trigger correctly (or rather consistently), I tried looking it up but I had issues understanding the key differences.
Good question, if your character's root node is a KinematicBody2D (renamed to CharacterBody2D in Godot 4 if that's what you are using) I recommend using the _physics_process method. If your character is based on something non physics related such as maybe an Area2D node, then the _process method is a good choice.
At the moment, my Player is based on the KinematicBody2D (using Godot 3.5) so I guess I stick with that one for now
When I first used _process to trigger the animations, I could clearly see "inconsistencies" when I wanted certain animations to trigger.
One example that I have is that I have a frame of an animation when the character jumps up (which is essentially 1 frame of my character looking up) not triggering when I wanted it to. This was pretty much remedied by using _physics_process.
It really felt like something unclogging in my head when I figure that one out.
What I need to figure out now is that I have a landing animation that I want to trigger when the character has landed, I have taken parts of "The Ledge grab: Designing the ledge detector" from the "Code a 2D Platform Game Character with Godot (Pro)". In that specific part of the tutorial, a "Floor Detector" (which is a Raycast2D pointing down) is used to detect the floor.
What I'm thinking is that I can use that Raycast2D to detect when a "collision" (or something) has happened with the floor, trigger the landing animation.