So I’m working on getting grenades to work, heres my current script, I wanted to use a trigger box collider but I couldn’t get it to stop dropping through the ground. Anyway this is my current script, it works good as the further an enemy is away the less damage it does to the enemy. Basically what the script does, is when it hits the ground the box collider grows really big and the bigger it grows the less damage it does to enemies. The problem is, is that it can’t hit multiple enemies at the same time, its only calculating damage for one enemy. Does anyone know how to fix this problem or a better more efficient way of doing this (this is for the iphone so I need something that works well and is really efficient).

EDIT: I posted the working code below.

``````var damage : float;
var change : float;
var sizeLimit : float;
var rb : Rigidbody;
var bc : BoxCollider;
var wTime : float;
var move : boolean = false;
var gravity : float;

function Start(){
rb = this.gameObject.rigidbody;
bc = this.gameObject.collider;
}

function Update(){
if(Input.GetKeyDown("r")){
//rb.useGravity = true;
move = true;
}
if(move){
transform.Translate(Vector3.up * -1 * gravity);
}
else{
transform.Translate(Vector3.zero);
}
}

function OnTriggerEnter(hit : Collider){
move = false;
//rb.useGravity = false;
if(hit.gameObject.tag == "Enemy"){
hit.gameObject.SendMessage("ApplyDamage", damage);
}
}

for(var a : int = 0; a < sizeLimit; a++){
bc.size.x += change*2;
bc.size.y += change;
bc.size.z += change*2;
bc.center.y += 0.5;
damage -= change*2;
yield WaitForSeconds(wTime);
}
}
``````

Personally I would just use physics.overlapsphere, then just calculate the sqrDistance of the hit objects to the object center to do your damage calculation. But if you really wanted to do it with a trigger sphere (a box makes no sense, explosions are not box shaped), you could parent an empty gameobject to the grenade and put a trigger sphere collider on that, and cache a reference to it in this script. That way the grenade could still have its collider so it doesn’t fall through the ground.

I don’t really see the point to expanding the sphere size though, it would in effect be the same as just having the default radius be the maximum size you were going to have it grow to and then calculate the distance of the hit objects to the grenade center to determine how much damage to do.

I know that explosions aren’t box-shaped I just wanted to test out the script, plus I thought that box colliders were a lot less taxing on the system than sphere colliders. Anyway, Thanks for the suggestions I’ll try that stuff out.

Nevermind I got it working, it seems that if a collider is not a trigger it can only calculate one hit at a time whereas a trigger can calculate many. So heres the working script in case anyone is interested.

``````var damage : float;
var change : float;
var sizeLimit : float;
var rb : Rigidbody;
var bc : BoxCollider;
var wTime : float;
var move : boolean = false;
var gravity : float;

function Start(){
rb = this.gameObject.rigidbody;
bc = this.gameObject.collider;
}

function Update(){
if(Input.GetKeyDown("r")){
//rb.useGravity = true;
move = true;
}
if(move){
transform.Translate(Vector3.up * -1 * gravity);
}
else{
transform.Translate(Vector3.zero);
}
}

function OnTriggerEnter(hit : Collider){
move = false;
//rb.useGravity = false;
if(hit.gameObject.tag == "Enemy"){
hit.gameObject.SendMessage("ApplyDamage", damage);
}
}