- Home /
GrenadeScript.cs Troubles
I have a script and when I "throw" the grenade I want all enemies in a certain radius to lose health. how would I do this?
Here is my script:
using UnityEngine;
using System.Collections;
public class Grenade : MonoBehaviour
{
public bool isRadius1;
public bool isRadius2;
public Collider radius1;
public Collider radius2;
public int damage;
public KeyCode throwKeycode;
public Rigidbody grenadeObject;
public Transform spawnPosition;
public float throwSpeed = 0f;
public float timer = 0f;
void Update ()
{
if(Input.GetKeyDown(throwKeycode))
{
StartCoroutine("ThrowGrenade");
}
}
IEnumerator ThrowGrenade ()
{
Rigidbody grenade = Instantiate (grenadeObject, spawnPosition.position, spawnPosition.rotation)as Rigidbody;
grenade.AddForce (transform.position * throwSpeed);
yield return new WaitForSeconds(timer);
DamageEnemies (radius1);
}
void DamageEnemies (Collider col)
{
}
}
Thanks!
Comment
Answer by code_warrior · Dec 02, 2014 at 05:40 PM
Hi,
I would simply use the OverlapSphere function.
Have a look here !
In my opinion that is the easiest way to check if an enemy was close to the grenade.
code_warrior
Your answer
![](https://koobas.hobune.stream/wayback/20220613173012im_/https://answers.unity.com/themes/thub/images/avi.jpg)