Best practice for melee attack platformer

Hey,
I’m trying to make my own Platformer game, using what I’ve learned in the Unity 2D course.
I’ve made animation for when my player is attacking with his sword (which is on his back during his running around). I was wondering what is the best way to make it so that if the player hits and enemy with a sword it will deal damage as in - should I have another collider component for the weapon that will move with the animation, should I create a new game object which will be the sword that will move with the animation? and if so, what’s the best way to make the game object / collider component move perfectly aligned with the sword in the animation?

My player has capsule collider 2D for his body and box collider 2D for his legs
image

Can’t upload the animation video that I made for reference, so I uploading the sprites that I used for the animation
image

Hi Yoad,

From what I see in your last screenshot, the sword looks like it should have its own collider. You could create a new game object with a BoxCollider2D, and you could animate that game object, so it moves with the sword sprite (animation).

Do not make anything “perfectly aligned”. The player will not notice the details anyway. If something does not work well, fake it until it feels right.

Please feel free to ask our helpful community of students for further advice over on our Discord chat server. Maybe one of them has implemented a sword in their game and knows a better solution. :slight_smile:


See also:

1 Like

In the end I found a technique with Physics2D.OverlapCircleAll, where i can have a “circle” in front of the player that every time the user hits the attack button, it checks if there is a collider inside this circle and if the object inside is enemy call the TakeDamage function on the those enemies (possible to have more than one enemy hit), as you said, I changed the timing until it felt “right” and “smooth” and I think it works good now.

Thanks for the help, it’s good to know its not suppose to be perfectly aligned and that it just needs to look good with a naked eye :slight_smile:

1 Like

Good job on making your idea work! :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms