First you need to understand what an RPG is: English RPG is Role Playing Games, RPG is a game form where players will role-play the characters in the game for turn-based or real-time combat.
Join the channel Telegram of the AnonyViet 👉 Link 👈 |
In this part we will create a character. Accompanied by a state machine with animation tree with Godot
Before that, please download the asset for yourself.
Here we will use Spritesheet.
Character
Sprite
First, we will put the character image into the Sprite as usual.
Notice the Attribute column (Inspector)
There will be VFrames and HFrames that those of you who have done with the previous tutorial will know.
- VFrames is the length of the Spritesheet
- Hframes is the verticality of the Spritesheet
Here:
- Hframes equal: 13
- Vframes equal: 16
After the set is complete, you will see
After setting 2, you will see the Player shrink as usual.
CollisionShape2d
Please set collisionshape as in the picture because this is a TopDown RPG game, not a game like the first Tutorial. This I will explain in a video on my channel, adjust like this because it will be more reasonable with titles. RPG game especially for objects in Godot.
Animation Player
You create the Animation Player node
Then create a new Animation that will include:
- idle : stand still
- run : run
- attack : attack
- dead : dead
Then create a new Track for yourself in animation IDLE
Then select Sprite
Then select Frames
Then right-click and select Insert Key and then Insert so that it is equal to the number of Frames of Animation
Here I will leave the photo with the number of Frames. I don’t know about other Engines but it’s like this with Godot
Then I will Insert Key with the number of Frames
In IDLE it’s 12 so I’ll insert 12 keys
You can use the bottom bar to adjust the size
Use the one below to adjust the track time
So I have finished editing, you guys do the same for the other ones.
State Machine With Animation Tree
State Machine: means state machine, with state machine you can manage your actions more easily and State Machine is different from Finite Machine. In Godot, there is an AnimationTree node that makes it easier for us to implement State Machine
You guys add AnimationTree node for yourself
Where TreeRoot chooses Nodestatemachine
Where Assgin chooses ANimationPlayer
Then Right Click to add all the animation you just created
Here then
Pink: Connect (arrow helps signal transmission)
Green: Clear
Purple: When this is present, Animation will run first when starting
Cam: Animation Finally just run the animation with this = Stop
Here I will Connect IDLE with other animations
As the picture above, Idle will connect via attack ie IDLE can transmit data through attack but cannot transmit because attack has no plan to talk to idle
As shown in the picture above, the two sides are connected. So when we are in IDLE, press attack it will run attack and after the attack finishes it will run to idle.
Please connect with the rest. If there is a dead one, just connect from idle to it because when you die it returns idle what else :D?
I will set start to idle so that when starting up the player will be stationary, right? so when standing still it will run animation idle. But when you first run the game, the player is standing still and running animtion run or attack.
And assign END at dead. What do you do when you die :D.
That’s it, the animation tree is done, next is the code
CODE
Here, we will use a bit of object-oriented code.
create yourself an enum containing state machine (state machine) including IDLE,RUN,ATTACK,DEAD and 1 state variable will be equal to IDLE.
Then next we have to define each state when acting. Here I will use match (same as case/switch) and change the name laydau Nhapvao Fort Trangthai_move
Next I need to access AnimatinoTree to be able to run Animation and turn on the AnimationTree Then I will have the command:
Then next I have to check if Player is stationary then it will run animation idle if not stationary ie move will run moving animation and check when player moves right it will rotate right and when player moves left then it will turn left.
Next I will create a Camera for easy viewing.
Then when you move, you will see it rotate right and left to run the animation.
That’s it, see you in the next post