- Home /
Area of effect melee attack for 3rd person game?
Hello everyone, I was just wondering if it was possible to create some sort of 'splash' damage (ie: damage in a range around the weapon) and if so, what would be the appropriate way to tackle it. I've looked around online, but almost every tutorial for melee attacking is based off single target attacks. So far the incomplete solutions I've come up with are:
Creating multiple raycasts (somewhat inefficient)
Creating collider meshes that will damage all enemies in its range (Not sure how I should go about with this, but might prove useful since I plan to create a very interactive hitting style like dragging out attack patterns by mouse.)
Damaging everyone within range? (Do I need to do some sort of for loop to apply damage to every enemy in range?)
Let's say I click the mouse to make the character swing a sword: I would want it to hit all enemies in the slice path, rather than locking on and only doing damage to the closest enemy.
I'm really new to Unity, so I'm just looking for any suggestions or examples. Thanks!
I'm also trying to figure this out. I've been trying to conceptualize the multiple raycasts but that runs into the issue of stopping when colliding with an object. This, in my case, is undesirable. I did see a function called SphereCast that may be what you're looking for. I'm looking for specific angles not an entire sphere.
Ins$$anonymous$$d of Physics.Raycast use Physics.RaycastAll. Same thing with Physics.SphereCast, Physics.CapsuleCast, and Rigidbody.SweepTest.
I don't feel like answering this since I don't have this specific kind of experience, but two ideas come to my $$anonymous$$d that might be helpful:
add collider component to the sword and make your player swing in a large area. This way you simply apply single-target damage to everyone that got hit by the swing.
spawn a large projectile (maybe some kind of circle-cut) that travels forward and grows (or simply spawn a growing sphere centered at player's position). It will damage everyone that touches its collider and will disappear after a short amount of time.
I would be careful with overly complicated or multiple raycasts, it may affect the game's performance.
Answer by TechSupportIncoming1 · Jul 17, 2015 at 07:08 PM
Here's a smart way to do it: when splash damage is on(assuming you have a flag for that) and using hit info, create a GameObject (assume it's called GO_SplashDMG, and could be empty) from a prefab at the point of hit.
This GO_SplashDMG prefab has a script attached to it that creates a collider (trigger) in the Start() method of the prefab script.
You then use the message OnTriggerStay() in the prefab script to do something like:
void OnTriggerStay(Collider other) {
if(obj.CompareTag("Enemy")){ // other.gameObject.tag == "Enemy" allocates memory :) Thanks @Vexe :D
DrainEnemyHP(hitPower);
StunEnemy(stunPower);
}
}
EDIT: OnTriggerStay may cause undesirable effects (in this example: continuous draining of HP and continuous stunning every physical frame as long as the generated prefab clone is not destroyed). So try using OnTriggerEnter if that didn't work out for you.
Do not forget to destroy the prefab automatically and almost instantly when using OnTriggerStay (or whenever you want if you're using OnTriggerEnter; i.e. the timer's duration you set for destroying the prefab clone is the time splash damage lasts for). Also have a rigidbody on each enemy.
Your answer
Follow this Question
Related Questions
How Can One Collider Recognize Contact With Another? 0 Answers
Enemy Health Damage 2 Answers
Editing a variable from another script on collision 3 Answers
How to make a falling object cause damage? 1 Answer
Damage trigger? 1 Answer