GameDevHQ Crash Course #34 2D HitBoxes and Collisions

Jason Dixon
3 min readApr 26, 2021

Collisions are essential for games, getting them acting correctly is not too difficult, however when you start adding hitboxes in you may start running into some complications, maybe you don’t want the hitbox to ‘see’ certain collisions.

This is our basic Collider, this will do for when we are registering getting hit, but for us to hit things this won’t do.

Once you have got your attack animation in place, we need to add a hitbox to the attack.

First thing we add an empty child item to the player.

to that we are going to add a collider and a rigidbody, setting the gravity to 0 so it doesn’t just fall away. Then we want to have it turned off, as we don’t want the collider to be registering when we are not attacking.

Now the next part it is a little longer, we need to move our collider to match our animation.

Since we made this the child of our player model , we can access the animations from the player, go to your animation panel and select your attack animation. We move the collider to the first frame we want the attack to become active.

Then we click record on the animation panel, go to your inspector and turn the child object to active, then go through your attack animation , moving and resizing your collider to how you would like it to hit , this may take a bit especially if you want to be highly accurate or have a longer more elaborate attack animation.

After you have finished adding all the collider frames for your attack animation. we now need to make sure it hits the enemies colliders and not the player ones, we don’t want to be damaging ourselves every time we attack.

For that we need to add some layers. In your inspector add a layer for your attacks to go on, also add one for enemies. Player is a default layer and we do not need to add one for that.

With those added go to edit->project settings-> physics2D then down on the Layer Collision Matrix we want to uncheck the collisions of player/attack, this will let the attack collider ignore any collider that we set onto the player layer.

--

--