Get all Objects from a Physics.OverlapSphere with Name X
heyhey. i wanna apply an explosion force to all objects in a Physics.OverlapSphere with the name "X". so my script snippet looks like this now:
Vector3 explosionPos = transform.position;
Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
foreach (Collider hit in colliders) {
Rigidbody rb = hit.GetComponent<Rigidbody> ();
rayhits.Add (hit.GetComponent<Collider>().gameObject);
//Add force to nearby rigidbodies
if (rb != null)
rb.AddExplosionForce (power, explosionPos, radius, 3.0F);
}
in there you can see that all "rayhits" with the component collider gets added to a list. all i wanna do now is to sort out all the other colliders and just add the one with the name "X" to that list.
Answer by InfiniBuzz · Oct 20, 2016 at 11:30 AM
One approach would be changing this:
rayhits.Add (hit.GetComponent<Collider>().gameObject);
to this:
Collider col = hit.GetComponent<Collider>();
if(col.gameObject.name.Equals("X")) {
rayhits.Add (col.gameObject);
}
Your answer
Follow this Question
Related Questions
Why does Physics.OverlapSphere not always return the same number of colliders? 1 Answer
OverlapSphere Problem. 1 Answer
Physics.OverLapSphere is not working Please help me to fix it or replace it 0 Answers
Very confused... Physics.OverlapSphere working, but not NonAlloc version? 2 Answers
Physics.OverlapSphere not detecting collision! Collision help. 1 Answer