Version: 2021.3
LanguageEnglish
  • C#

Physics.OverlapSphere

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public static Collider[] OverlapSphere(Vector3 position, float radius, int layerMask = AllLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

Parameters

position Center of the sphere.
radius Radius of the sphere.
layerMask A Layer mask defines which layers of colliders to include in the query.
queryTriggerInteraction Specifies whether this query should hit Triggers.

Returns

Collider[] Returns an array with all colliders touching or inside the sphere.

Description

Computes and stores colliders touching or inside the sphere.

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: Layer mask particularly "Casting Rays Selectively" for a detailed example of Layer masking.