Nerd For Tech
Published in

Nerd For Tech

Hitbox Attack System Unity2D

Now that there are enemies within our game, let’s get to work on having out sword have the ability to kill off the enemies. To start, we need to look at how we can create a hitbox with the attack animation swing of our character.
To start, we are going to create a new sprite as a child object to our sprite model and go through the frames of the animation so that we can pair the swords movement with that of the hitbox we are creating for it:

Once we have mapped out the majority of the animation, we will have our box trace is like so:

Finally, we are just going to have our box collider off at all times, aside from when we use our attack:

Now, let’s get a script made for our hitbox and check to see if it hits the player:

In order to check, we just run a quick debug.log and let unity know to tell us the object that we hit, and as we can see, we are hitting our player. In order to fix this, we are going to create a couple new layers to work with. We will create a sword and a player layer, and within the settings of those layers, we are going to have them not interact with each other:

Now that we have it set up so our sword doesn’t interact with the player, let’s just make sure that it can detect our enemies:

There we have it, we have now created a hit box for our sword swing animation that doesn’t interact with our player, but will make contact with our enemy units providing we have box colliders on them. Next up, we will take a look at how we can set up an Idamagable interface on our enemy units.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store