Thursday, March 28, 2024

Animating the sprite part4

 More entries on animated sprites, now it's turn for hitbox and hurtbox where it depends on the animated frame. Again, we'll be getting help from: https://www.youtube.com/watch?v=vDbEfmPcv-Q

We are going to start renaming a few layers and masks so we can deal with them more easily in the long run.
Key part here is understanding and differentiating what is the use of each ( https://docs.godotengine.org/en/stable/tutorials/physics/physics_introduction.html ):

Layer: describes the layers that the object appears in.
Mask: describes what layers the body will scan for collisions


Go to Project > "Project Settings" and, in the general category, scroll down until you see "Layer names". Select the "2d physics" option for this.

We'll name it in this order: World, Player, PlayerHurtbox, EnemyHurtbox


That will make our life easier when we want to know what collides with.

- Now we are going to create one scene for hurtbox and another for hitbox.


- Add a collisionShape2d child note to each of them


Note: there's a warning telling us that the node needs a shape, but we're not going to create one here. The explanations for it is inheritance, we want them as a base to attach to other nodes like the player or the enemies. That's where we are going to work with that.

- Attach the hitbox scene to the player


- Right-click on the hitbox scene you've attached to the player and select "Editable Children"


- Now we are going to create the shapes, in the child of the hitbox node from the player.


- Before working with the shape, we want a Position2D node to use a visual indicator for the position of the item that will contain the hitbox, we are going to call it "HitboxPivot"


- Time to go back to our animation to add the hitbox. Select your animatedTree


- And then select your AnimationPlayer


You want to use the frame that better displays the range of the collision shape you want to create. Let's do an example with the attackDown here.

- Select a frame to display and go to your Hitbox to edit its shape


After editing your shape, you should have something like this


- You have to key the rotation of that hitboxPivot at the first frame of the animation


- Save and repeat the process for all the attack positions (left, up, down and up)


- But the hitbox is now set to active all the time, and we want it to only be active during the attack input. To do that, select your collisionShape in the Hitbox and disable it.


- Now go to your animation frames and key the disable property, so it sets off the collision during the attack frames and disables the collision after the animation ends finishes.


This works properly now, but before finishing, remember the layers from the beginning of the entry? It's time to set them (the layers were: 1 = World, 2 = Player, 3 = PlayerHurtbox, 4 = EnemyHurtbox).

- Put your player in his layer, 2. And select where he will scan for collisions, 1 and 4.



That's all for this animated sprites section.

No comments:

Post a Comment