Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


SphereCast

Casts a sphere along a ray and returns detailed information on what was hit.
Read time 2 minutesLast updated 10 days ago

Definition

  • Type: Method
  • Namespace: UnityEngine
  • Assembly: UnityEngine.PhysicsModule

SphereCast(Vector3, float, Vector3, RaycastHit, float, int, QueryTriggerInteraction)

Casts a sphere along a ray and returns detailed information on what was hit.
public bool SphereCast(Vector3 origin, float radius, Vector3 direction, out RaycastHit hitInfo, float maxDistance = Infinity, int layerMask = -5, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)

Parameters

origin

The center of the sphere at the start of the sweep.

radius

The radius of the sphere.

direction

The direction into which to sweep the sphere.

hitInfo

If true is returned,
hitInfo
will contain more information about where the collider was hit. (Additional Resources: RaycastHit).

maxDistance

The max length of the cast.

layerMask

A Layer mask that is used to selectively filter which colliders are considered when casting a sphere.

queryTriggerInteraction

Specifies whether this query should hit Triggers.

Returns

Type

Description

boolTrue when the sphere sweep intersects any collider, otherwise false.

Remarks

Additional Resources: Physics.SphereCast.

SphereCast(Vector3, float, Vector3, RaycastHit[], float, int, QueryTriggerInteraction)

Cast sphere along the direction and store the results into buffer.
public int SphereCast(Vector3 origin, float radius, Vector3 direction, RaycastHit[] results, float maxDistance = Infinity, int layerMask = -5, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)

Parameters

origin

The center of the sphere at the start of the sweep.

radius

The radius of the sphere.

direction

The direction into which to sweep the sphere.

The buffer to save the results to.

maxDistance

The max length of the cast.

layerMask

A Layer mask that is used to selectively filter which colliders are considered when casting a sphere.

queryTriggerInteraction

Specifies whether this query should hit Triggers.

Returns

Type

Description

intThe amount of hits stored to the
results
buffer.

Remarks

Additional Resources: SphereCastNonAllloc.