Melee Attack

Could someone tell me how to make a melee attack script?

A bit vague Sam - if you ask a good question you’ll get a good answer. A little more detail would help.

EDIT: OK - maybe it was only vague to me. Thanks Pod :wink:

The simple option is:

  1. Stick a collider onto your character’s weapon (a sword, say).
  2. Check when it hits things.

If your character has no image / weapon (you just want to be able to bonk things that are close to you) then stick a collider in the appropriate place.

One of my fond dreams is to write a melee action game where swords actually bounce off shields and other swords … versus acting like short-range shotguns as they do in pretty much every “fantasy” or martial arts game I’ve ever seen.

But how could I make it apply damage?

Using colliders is the best

When the colliders collide (like your sword collider with an enemy collider) you do all the hit logic, calculate the damage, remove the armor, maybe calculate if it misses or do a critical hit, etc.

That’s it :smile:

.ORG

Ahaa, that sounds good, a lot better then shooting slices.

But how do you make the animation play all frames and how do you setup a “reload” time?

If I use Input.GetButtonDown then my animation won’t play all the way. If I use Input.GetButton then the animation stops when I release the button and with Input.GetButtonUp it also doesn’t play (or only for just 1 frame).

You would set up a reload time variable and set it to zero. Then everytime you hit the fire button, it checks to see if the variable is equal to or above the approximate time of your animation, and if it is, then play the animation and reset the variable to zero. If its below the time of the animation (meaning its still playing) then do nothing.

Hope that made sense. You can look at the machine gun script in the fps tutorial and see how they set up their reload function.

Well i found that one way to do it, is to make an empty object and apply a box collider to it, then set the object to a trigger.
Then what you could do, is create that box at a position when your character attacks, or attach it to some object (havent quite figured that out yet).
You would also have to check for collisions against enemies on the the hit box object that you’ve created by using a script on the OnTriggerEnter function.
I keep track of the enemies ive hit on the hit box object so that one box hits an enemy once, rather than every frame, i keep track of the enemies by simply making an int array that holds instance ID’s of enemies, so when i succesfully hit an enemy it adds that enemy to the list to stop it from being hit again.
To apply damage, you would simply have to set a damage variable to the hit box when created, check who got hit, and subtract their hp from the damage you’ve set.

Or you can be even cooler and use Animation.CrossFadeQueued to make a chain of animations… Create combos and stuff :slight_smile:

Also - think about making the code of the animation either in coroutines or in the update function… Not directly inside your GetMouseWhatever() functions… You need your animation to play out regardless of what you’re doing, and maybe just check what changed. An Idle cycle, for example, should be playing even if your user does nothing… You can send a command to start an animation, or make it stop based on the input of the user, but the actual crossfading, starting and stopping, in my opinion, should be done in some function/functions that is/are dedicated for all the animation code.

I have been trying to get the technique listed in this thread to work and have been having some trouble. I attached a collider to my weapon and set it to be a trigger. I then have my character swing the weapon via his attack animation. When the weapon hits another character controller’s capsule it does not trigger, however, when it hits a cube that has a collider it does trigger. How can I get the weapon to trigger when it hits another character controller?

Thanks!

Ive noticed that character controllers don’t register collisions like everything else. Which is annoying and currently giving me a problem as well. The only way I’ve found that character controllers register collisions is through OnControllerColliderHit(). But thats probably not useful in your situation.

I found a solution to my problem…

A collision will not trigger unless one of the colliders is a rigidbody. So I made my weapon have a rigidbody with isKinematic set to true and this allowed me to detect collisions between it and a CharacterController.