Melee attack

Hi there!

I was writing a melee attack script, and I just wanted to know if there was a better method than mine to do it.

My idea was to attach a box collider to the sword, and then, during attack, check collisions with game objects tagged “Enemy”, reduce their HPs and move them back.

Trust me, use raycast. The move back part you would want to handle with animation or add force.

Well, but what if I want precise collisions with the sword?

For example a circular attack, that would not work with raycast. Yeah I could use SphereCast, but just wondering why my initial idea is not a possible right approach.

I recently built a melee system and for reasons i can’t explain we couldn’t get the collision to work while being pushed through an object (ie) animation or rotation etc, no matter how or what collision/trigger function.

Raycast worked like a charm.

What do you mean by circular attack?

If your character spins around and hits enemies standing all around it, then the raycast on the sword will slice right through them like butter lol.

I see, so are you’re raycasting say, from the hilt of the sword down it’s length to the tip? I can see how this would be a lot faster than a seperate physics object… thanks for the tip!

I put a hitbox (box collider) on the blade of my weapons which is instantiated when a player gives an attack input. The animation carries the weapon through the swing (arch swing, whirlwind attack, double slash animations), if the hitbox hits an enemy collider it triggers a damage function similar to a projectile script…

At first it would hit like 4+ times with each swing as the sword went through the arm, into the body, out of the body and through the other arm. Took some tweaking. But it’s just a new variable and function I added to the missilelauncher and projectile scripts from one of the game examples.

Your best bet would be to wait until you get the input to attack (Fire1 etc) and then instantiate an invisible collider object and make it a child of the sword object, so that it fits along the blade, and have an OnCollisionEnter script that checks to see if we’re hitting another character/shield/etc. When it collides, have the script execute the desired effect of the impact and then destroy the collider gameobject. If it doesn’t collide with anything after a given time period (the time it takes to swing the weapon, for instance) destroy the object.

That way you will only hit the first thing they come into contact with (if they’re using a shield to block, for example, it won’t hit the player as it will destroy before it hits the player) and only when they’re actually attacking.

There’s a problem here, I used that. But how would you hit 2 or more enemies?
And if an enemy blocks but somehow the collider of the shield isn’t big enough and you still hit the enemy.
It would be realistic but you don’t want someone who’s facing the player, blocking and still getting hit.

I personally use “Vector3.Distance” and attack timer, use the method “Distance” is simpler to use melee, like zombies.