f
functionSam

Need help with to get going with 2D animations

Hello everyone!

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.

Context

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.

The Problem

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

  • f
    functionSam replied

    I think I solved it.

    Context

    1. So I have my scene called "Player", which in turn has a script called "Player.gd".
    2. I want to trigger the animations from the "Player.gd" script but to trigger them (see "2D Movement in Godot in Only 4 minutes") I need a velocity.
    3. The velocity I want access to can be found in the "Move" state, in the script called "Move.gd", the structure in the Player scene is: "Player" --> "StateMachine" --> "Move".
    4. Inside the "Move.gd" script", there's a variable called "velocity": var velocity: = Vector2.ZERO.
    5. To get access to velocity we want access to the node (Move) that was created during the tutorial, so create a variable for it, in my case: onready var move: State = $StateMechine/Move.

    Now I create a function to use it, for example:example.png


    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.
  • a
    andrewjhaugen replied

    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!

  • f
    functionSam replied

    aandrewjhaugen

    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.

  • a
    andrewjhaugen replied

    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.

  • f
    functionSam replied

    aandrewjhaugen

    At the moment, my Player is based on the KinematicBody2D (using Godot 3.5) so I guess I stick with that one for now

  • a
    andrewjhaugen replied

    That's what I would go with. You can mix and match with both _physics_process and _process if you need to. For example if you have some player functionality that doesn't really depend on the physics engine you can put that in the _process method.

  • f
    functionSam replied

    aandrewjhaugen

    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.

  • a
    andrewjhaugen replied

    ah yeah that makes sense, since the movement is based on the physics engine, with the animations triggering in _process instead of _physics_process there is the possibility for that desynchronization you were talking about. Glad it's working as intended now.

  • f
    functionSam replied

    aandrewjhaugen

    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.

    1 love