Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


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

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

Type

Description

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

Remarks

Allocates memory. Consider using Physics.OverlapSphereNonAlloc instead.

Examples

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"); } }}