Welcome to Lesson 3 in the RPG game programming series with Godot: 3
Join the channel Telegram belong to Anonyviet 👉 Link 👈 |
Okay, in this article I will guide you to attack with Animation Player & Hitbox & Hurtbox and Object with Godot
And let’s start
Attack
We will create a new sense to make hitbox to check when we attack and hurbox to check when the hitbox it collides with blood loss.
Hitbox & Hurtbox
Create the hitbox and the original button is Area2D and then add the collisionshape2d.
Then create a new script
Here I will use the Export variable to export to the Inspector for easy adjustment for each character. You can adjust the language outside Godot’s interface to Vietnamese for ease
Then save it.
Then create the new sense is Hurtbox then save like hitbox and no need to create script because we will connect the signal to the Player
Click and then beat
We have to check if clicking, then attack. You can edit the input if lazy, use as photo: etc. Godot has many options!
Then the state will be equal to Attack.
In Attack: We will create a new function is TrangThai_attack will have code running Animtion Attack
Then next you call it on Attack: okay.
Then when we attack we have to set Speed = 0 so that when it attacks it will not move
After the attack is completed, it will stand still because it has not returned to IDLE state so we have to set it back.
I will create a new function
Then in Animationplayer> Animation Attack> Add track method (this is the track that will run the Godot function with this convenient: 3)
Then select Player because the new Player has the code to run: D
Then choose the jaw
Then adjust it to the end because at the end of animation = run the jaw
And so you will move normally
Instance & Animation Player
Next we will instance it in Godot. Instance I don’t know what to call Vietnamese @@.
Then click Add 2 hitbox & Hurtbox then right -click 2 press Editable as Children so we can adjust it as a sub -button and set Collishape.
Well before that, we had to set the mask/layer. Visit Project> Project Setting> 2D Render / 2D Physics
You guys set like me. Set for both 2D Render and 2D Physics
Then in the player’s hitbox on the attribute table> Collision, I will remove the tick in the layer and in the mask will tick it collide with Hurtbox_e, which means that when the player attacks it will collide with Hurtbox of the enemy.
Then at Hurtbox Layer, you ticked in the hurbox_p box in the blank mask
Next is to adjust the collisionshape2d of Hurtbox to fit the character.
Then will set Collisionshape2D of hitbox with animationplayer.
In hitbox’s collisionshape2d, press the key to add track quickly
Then you will watch the position when it attacks / off.
Because it has not been attacked, I will leave disabled = true on second seconds and then check the next few seconds if you come to the attack, set disabled = false and then after drawing, disabled = true
Then running again will find it okay and Perfect: 33. I like this in Godot extreme
Object
Next we will create object with Godot.
Download link: https://elthen.itch.io/pixel-art-destructible-Objects
Create a new sensing
Here created:
Area2d> Sprite – Collisionshape2d – Animationplayer
In Sprite, put the image in and I feel a bit loud, you set the scale accordingly.
Vframes = 12
Hframes = 7
Fuzzy Reimport again.
Then next in the Animationplayer creates 2 animations: hit and Destroy. The hit is animation when hitting and Destroy is animation when destroyed.
Here I will be the box you want to change.
Hit: 7-9
Destory: 10-13
At the Animation Destroy I will apply the similar Call Method above.
Destroy () contains queue_free () statement
Then create a new script. Here we do not need hitbox or hurbox because this barrel is already an Area2D button.
We will set Collisionshape2D for it and connect the signal. But note that if you follow the Area2D button like I will have a disadvantage when the player collides with it. If you use staticbody to make it and add Hurtbox, it will be better.
Connect Area Enterd signal.
I will create HP = 10 variable to have a base when the player attacks.
Then at the signal function, I will check if the name Area = “Hitbox”, the HP will be equal to the damage of Area minus HP. The print is that I use it to check HP to see if I hit it yet.
Then set the layer into Hurtbox_e and uncheck the mask.
Next we need to check if the player attacks, we will be saggy hit and when the blood runs out of Animation Destroy
Here I will function _Process (): to check every second if HP <= 0, then run animation and cancel itself
Then next to make sure you at the Animation hit, click the circle under the picture so that when it starts it runs Animation hit, I have to Destroy
Note: Object, at first I created stupid so I sympathized: v. When running the game Đbject will not collide with the player if you want you to adjust to staticbody2d or kinematicbody2d and add hurbox> connect the signal and apply the code as above.
See you in the next post of RPG game programming series with Godot