- Home /
How to Destroy gameObjects and deal damage within a radius except player ?
Hello,
Ive been wondering how to I achieve a destruction radius that also does damage to other gameobjects except my player. In short my player is launching a missile, after a few secconds that missile explodes , and when it explodes i want it to destroy all gameobjects within a radius or deal damage to them except the player.
I`ve been looking at the Physics.OverlapSphere but i cant get my way to work it.
thanks guys.
Answer by Taylor-Libonati · Dec 27, 2017 at 08:15 PM
Physics.OverlapSphere is a good way to go. You will be doing almost exactly what the example script is doing although I would suggest that instead of using SendMessage you call an actual function. Example script
So I will break down the general idea. You get a set of colliders that are in range, then you loop through them and if it is an object that you can hit you apply the damage or just destroy them.
This gives you a list of colliders
Collider[] hitColliders = Physics.OverlapSphere(center, radius);
This loops through them
for(int i=0; i<hitColliders.Length; i++){
This checks if it is an object you want (using tags, but you could do this other ways)
if(hitColliders[i].gameObject.tag == "Enemy"){
This calls a function on the object
EnemyBaseClass enemy = (EnemyBaseClass)hitColliders[i];
if(enemy != null){
enemy.TakeDamage();
}
If you want more specific help then that you will have to post your code so we can see where you are going wrong.
thanks my friend but i get an error athe the for loop " cs0019 '<' cannot be applied to operands of type int and unityEngine.Collider[]
That's probably just a syntax error. I didn't test my code so there might be little things you need to change. If you post your code I or other people will be able to help you better. Also there are a lot of Unity tutorials that $$anonymous$$ch you how to do things like missiles and grenades if you want to read up on the subject.
Sorry for the late reply but here is my rocket code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class RocketPlayer : $$anonymous$$onoBehaviour {
public Rigidbody rocketRb;
public GameObject explode;
public float radius = 10f;
public float speed;
public float life = 2;
private Vector3 center;
public bool isDeadRocket;
void Start ()
{
isDeadRocket = false;
rocketRb.velocity = transform.forward * speed;
}
void Update ()
{
StartCoroutine (RocketDeath ());
}
IEnumerator RocketDeath ()
{
yield return new WaitForSeconds (life);
Destroy (gameObject);
Instantiate (explode, transform.position, transform.rotation);
}
void Explode ()
{
center = transform.position;
Collider[] hitColliders = Physics.OverlapSphere (center, radius);
for (int i = 0; i < hitColliders; i++)
{
if (hitColliders[i].gameObject.tag == "Cube") {
Destroy (gameObject.tag == "Cube");
}
}
}
}
Answer by Consul-Livius · Jan 09, 2018 at 10:25 AM
I resolved it on my own, i just instantiated an empty game object with a sphere colliders that does the job upon rocket death.
Thanks for the help!
Your answer
![](https://koobas.hobune.stream/wayback/20220612145330im_/https://answers.unity.com/themes/thub/images/avi.jpg)