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.
![[Godot RPG] #3: Attack & Object 36 [Godot RPG] #3: Attack & Object 35](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-04-18-45.png)
Then create a new script
![[Godot RPG] #3: Attack & Object 37 [Godot RPG] #3: Attack & Object 36](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-04-19-35.png)
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
![[Godot RPG] #3: Attack & Object 38 [Godot RPG] #3: Attack & Object 37](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-04-20-10.png)
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
![[Godot RPG] #3: Attack & Object 39 [Godot RPG] #3: Attack & Object 38](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-04-21-04.png)
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!
![[Godot RPG] #3: Attack & Object 40 [Godot RPG] #3: Attack & Object 39](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-04-54-26.png)
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
![[Godot RPG] #3: Attack & Object 41 [Godot RPG] #3: Attack & Object 40](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-05-00-04.png)
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
![[Godot RPG] #3: Attack & Object 42 [Godot RPG] #3: Attack & Object 41](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-05-01-12.png)
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
![[Godot RPG] #3: Attack & Object 43 [Godot RPG] #3: Attack & Object 42](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-05-03-42.png)
Then in Animationplayer> Animation Attack> Add track method (this is the track that will run the Godot function with this convenient: 3)
![[Godot RPG] #3: Attack & Object 44 [Godot RPG] #3: Attack & Object 43](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-05-04-18.png)
Then select Player because the new Player has the code to run: D
![[Godot RPG] #3: Attack & Object 45 [Godot RPG] #3: Attack & Object 44](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-05-04-27.png)
Then choose the jaw
![[Godot RPG] #3: Attack & Object 46 [Godot RPG] #3: Attack & Object 45](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-05-06-02.png)
Then adjust it to the end because at the end of animation = run the jaw
![[Godot RPG] #3: Attack & Object 47 [Godot RPG] #3: Attack & Object 46](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-05-06-13.png)
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.
![[Godot RPG] #3: Attack & Object 48 [Godot RPG] #3: Attack & Object 47](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-04-23-54.png)
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
![[Godot RPG] #3: Attack & Object 49 [Godot RPG] #3: Attack & Object 48](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-04-26-03.png)
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.
![[Godot RPG] #3: Attack & Object 50 [Godot RPG] #3: Attack & Object 49](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-04-26-58.png)
![[Godot RPG] #3: Attack & Object 51 [Godot RPG] #3: Attack & Object 50](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-04-26-50.png)
Then at Hurtbox Layer, you ticked in the hurbox_p box in the blank mask
![[Godot RPG] #3: Attack & Object 52 [Godot RPG] #3: Attack & Object 51](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-04-28-40.png)
Next is to adjust the collisionshape2d of Hurtbox to fit the character.
![[Godot RPG] #3: Attack & Object 53 [Godot RPG] #3: Attack & Object 52](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-04-29-47.png)
Then will set Collisionshape2D of hitbox with animationplayer.
In hitbox’s collisionshape2d, press the key to add track quickly
![[Godot RPG] #3: Attack & Object 54 [Godot RPG] #3: Attack & Object 53](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-04-31-41.png)
![[Godot RPG] #3: Attack & Object 55 [Godot RPG] #3: Attack & Object 54](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-04-31-54.png)
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
![[Godot RPG] #3: Attack & Object 56 [Godot RPG] #3: Attack & Object 55](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-04-33-41.png)
![[Godot RPG] #3: Attack & Object [Godot RPG] #3: Attack & Object](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-04-34-58.png)
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
![[Godot RPG] #3: Attack & Object 58 [Godot RPG] #3: Attack & Object 57](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-04-38-18.png)
In Sprite, put the image in and I feel a bit loud, you set the scale accordingly.
Vframes = 12
Hframes = 7
![[Godot RPG] #3: Attack & Object 59 [Godot RPG] #3: Attack & Object 58](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-04-41-01.png)
Fuzzy Reimport again.
![[Godot RPG] #3: Attack & Object 60 [Godot RPG] #3: Attack & Object 59](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-04-41-27.png)
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
![[Godot RPG] #3: Attack & Object 61 [Godot RPG] #3: Attack & Object 60](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-05-14-36.png)
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.
![[Godot RPG] #3: Attack & Object 62 [Godot RPG] #3: Attack & Object 61](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-04-49-11.png)
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.
![[Godot RPG] #3: Attack & Object 63 [Godot RPG] #3: Attack & Object 62](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-05-08-42.png)
Then set the layer into Hurtbox_e and uncheck the mask.
![[Godot RPG] #3: Attack & Object 64 [Godot RPG] #3: Attack & Object 63](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-05-10-47.png)
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
![[Godot RPG] #3: Attack & Object 65 [Godot RPG] #3: Attack & Object 64](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-05-12-39.png)
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
![[Godot RPG] #3: Attack & Object 66 [Godot RPG] #3: Attack & Object 65](https://anonyviet.com/wp-content/uploads/2020/08/08-08-2020-05-16-07.png)
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






