position | Center of the sphere. |
radius | Radius of the sphere. |
layerMask | A Layer mask that is used to selectively ignore colliders when casting a ray. |
queryTriggerInteraction | Specifies whether this query should hit Triggers. |
Returns an array with all colliders touching or inside the sphere.
// Call a damage function on all objects caught in the // radius of an explosion. function ExplosionDamage(center: Vector3, radius: float) { var hitColliders = Physics.OverlapSphere(center, radius); for (var i = 0; i < hitColliders.Length; i++) { hitColliders[i].SendMessage("AddDamage"); } }
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { void ExplosionDamage(Vector3 center, float radius) { Collider[] hitColliders = Physics.OverlapSphere(center, radius); int i = 0; while (i < hitColliders.Length) { hitColliders[i].SendMessage("AddDamage"); i++; } } }