SIMPLIEST Area Damage Script (Java)

Good day everyone!

I’m bothering you with a fairly - I guess - easy problem, which I can not solve alone.

I’m a total beginner and trying to make a game SIMPLIEST AS POSSIBLE. A 2D shooter which has to be like Kalash - crudely simple and functional.

Lately I ran onto a problem. That is - a melee / grenade script. Both are supposed to deal the same amount of damage to all targets in the weapon range - a Trigger Cube.
*(while melee is invisible, a grenade will spawn an explosion which I’ll make later - that part is simple).

The Problem!

I’m working on tutorials for now and know some basics about Java. After I made the prototype script for the knife (invisible Trigger Cube in front of the character), it only “scans” for the first target in the Trigger Zone, the rest are unaffected.

Thy Script!

#pragma strict

var TheDamage : int = 50;
var WeaponSlotActive : GameObject;
var AttackCooldown : int;
var AttackInterval : float = 05;
private var PoorBastard : GameObject;

function OnTriggerStay (other : Collider) {

PoorBastard = other.gameObject;

if (Input.GetButtonDown("Fire") && WeaponSlotActive.activeSelf == true && AttackCooldown == 0){

	AttackCooldown = 1;
	PoorBastard.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
	yield WaitForSeconds (AttackInterval);
	AttackCooldown = 0;

Simple. When Knife is active, send damage on pressing “Fire” button to the Poor Bastard in the weapon range. Wrote it myself and with my Noob Status I’m bloody proud of it.

So, to solve the one-target problem, I googled it. Too bad, under all tags ("Unity aoe damage, splash damage, grenade, multi target trigger etc.) it finds only scripts, threads and discussions about things with no explanation, no sense or a million details with damage falloff, barriers between targets etc. - couldn’t find the SIMPLE multi target script script.

The Question!

Will someone better oriented in Unity be brave enough to post (with a newbie explanation), what var and command should I add and where in the above code to make it multi-target?

What say you!?

This isn’t an answer to the question you’re asking, but a different (and potentially simpler) way of solving your problem.

You don’t necessarily need to send a message to the object being attacked. You could make a script for the enemy containing a variable for health and a simple function in that script which reduces their health whenever the knife trigger enters it. Then if you attach that script to each enemy they’ll all be affected by the knife, and you won’t have to worry about the knife scanning for each target in range.

Well, that seems like a way out, but I want to stick to one-script solutions. Adding a script to every new NPC or destructible object would guarantee that I would forget to add it on some point. :stuck_out_tongue: Also, if it was a “trap” that deals damage on trigger enter, then it’s ok, but I want to deal damage when they are IN trigger AND when I press a button for knife attack. That complicates it. :wink:

BUT! I won the life lottery and at last learned how to use the “Overlap sphere” option.

The script part with it sending damage to targets, for future generations:

#PART 1:
var MeleeRange = float; //This goes on the beginning of script, in vars
var TheDamage = int; //Knoife Domoige. Derp.

#Part 2:

//This goes to the bit where you deal damage. no aditional vars required:

	var PoorBastards = Physics.OverlapSphere(center, MeleeRange);
	for (var i = 0; i < PoorBastards.Length; i++) {
	 if (PoorBastards*.gameObject.tag == "Enemy"){*

_ PoorBastards*.SendMessage(“ApplyDamage”, TheDamage, SendMessageOptions.DontRequireReceiver);_
DONE. :smiley: Thanks for support!