Welcome to lesson 3 in the series of RPG game programming with Godot :3
| Join the channel Telegram of the AnonyViet 👉 Link 👈 |
Okay, in this post I will guide you to attack with animation player & Hitbox & Hurtbox and Object with Godot
And let’s get started
Attack
We will create a new Sense to be a Hitbox to check when we attack and a hurtbox to check that when the hitbox it collides, it loses health.
Hitbox & HurtBox
Create the Hitbox and the root node 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 change the language outside of Godot’s interface to Vietnamese for easy
![[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 and you’re done.
Then create a new Sense as HurtBox then save it as Hitbox and don’t need to create a 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)
Tap then hit
We have to check if we click to attack. You can Edit the Input, if you’re lazy, use it like a picture :vv. 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 status will be equal to ATTACK.
In ATTACK: we will create a new function called trangthai_attack which will have code to run 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 attacking we have to set speed = 0 so that when attacking it won’t 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 done, it will stay still because it has not returned to the 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 which has this convenience :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 choose Player because the Player only has the code to run:
![[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 select the function
![[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 the animation = run the function
![[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 again
Instance & Animation Player
Next we will Instance it in Godot. Instance I don’t even know what to call Vietnamese anymore@@.
Then click add 2 Hitbox & Hurtbox then right click on 2 click Editable as children so we can adjust it as a child node and set collishape too.
![[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 have to set the mask/layer first. Go to Project > Project Setting > 2D Render / 2D Physics
Please 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 uncheck the layer and in the Mask will tick it to collide with Hurtbox_E ie when the player attacks it will collide with the enemy’s hurtbox.
![[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 in the HurtBox Layer, you tick the box Hurtbox_P 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 next will set collisionshape2D of Hitbox with AnimationPlayer.
In Hitbox’s Collisionshape2D, click on the key to add a quick track
![[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)
You will then position when it attacks to turn it off/on.
Since it hasn’t attacked yet, I will leave Disabled = true at 0 seconds and then check the next few seconds if it comes to attack action, set disable = false and then after withdrawing the sword, 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 run it again and it will be fine and perfect :33. I love this in Godot
Object
Next we will create object with Godot.
Download link: https://elthen.itch.io/pixel-art-destructible-objects
Create a new Sense
Here, create:
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 the sprite, please put the image in and I think it’s a bit big, you should 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)
If not, then reimport it.
![[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 AnimationPlayer create 2 animations Hit and Destroy. Hit is the hit animation and Destroy is the destroyed animation.
Here I will be the Box you want to change.
Hit : 7-9
Destory:10-13
In animation destroy I will apply the same call method above.
destroy() contains the 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 don’t need Hitbox or Hurtbox because this box is already an area2d node.
we will set collisionshape2D for it and then connect the signal. but note that if you follow the area2D Button like me, you will be at a disadvantage when the player collides with it. If you use StaticBody to make it and then add Hurtbox to it, it will be better.
Connect signal Area enterd offline.
![[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 the variable hp = 10 to have the base when the player attacks.
Then in the signal function, I will check if the name area = “Hitbox”, then the hp will be equal to the damage of the area minus hp. The print is what I use to check the HP to see if I have entered it, Godot.
![[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 to 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 it will stop the animation hit and when the health runs out run the animation destroy
Here I will function _process(): to check every second if hp <= 0 then it will run animation and then self-destruct
![[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 are at ANimation hit, click the circle below the image so that when it starts it runs the animation hit, not 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 was stupid at first, so I sympathize :V. When running the game Ọbject will not collide with the player, if you want you have to change it to StaticBody2D or KinematicBody2D and then add hurtbox > connect the signal and then apply the above code.
See you in the next article of the series of RPG game programming with Godot










