OverlapSphere(Vector3, float, int, QueryTriggerInteraction)
Computes and stores colliders touching or inside the sphere.
Read time 1 minuteLast updated 6 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.PhysicsModule
public static Collider[] OverlapSphere(Vector3 position, float radius, int layerMask, QueryTriggerInteraction queryTriggerInteraction)
Parameters
Center of the sphere.
Radius of the sphere.
A Layer mask defines which layers of colliders to include in the query.
Specifies whether this query should hit Triggers.
Returns
Type | Description |
|---|---|
| Collider[] | Returns an array with all colliders touching or inside the sphere. |
Remarks
Additional Resources: Physics.AllLayers. Allocates memory. Consider using Physics.OverlapSphereNonAlloc instead.
using UnityEngine;using System.Collections;public class ExampleClass : MonoBehaviour{ void ExplosionDamage(Vector3 center, float radius) { Collider[] hitColliders = Physics.OverlapSphere(center, radius); foreach (var hitCollider in hitColliders) { hitCollider.SendMessage("AddDamage"); } }}
Additional Resources: Ray cast with layers section of Use of layers in Unity.