Legacy Documentation: Version 5.1
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Physics.SphereCast

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

Sumbission failed

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

Close

Cancel

Switch to Manual
public static function SphereCast(origin: Vector3, radius: float, direction: Vector3, out hitInfo: RaycastHit, maxDistance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers): bool;
public static bool SphereCast(Vector3 origin, float radius, Vector3 direction, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers);

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 (See Also: RaycastHit).
distance The length of the sweep.
layerMask A Layer mask that is used to selectively ignore colliders when casting a capsule.

Returns

bool True when the sphere sweep intersects any collider, otherwise false.

Description

Casts a sphere along a ray and returns detailed information on what was hit.

This is useful when a Raycast does not give enough precision, because you want to find out if an object of a specific size, such as a character, will be able to move somewhere without colliding with anything on the way. Think of the sphere cast like a thick raycast. In this case the ray is specified by a start vector and a direction.

Notes: SphereCast will not detect colliders for which the sphere overlaps the collider. The sphere cast does not work against colliders configured as triggers. If you move colliders from scripting or by animation, there needs to be at least one FixedUpdate executed so that the physics library can update it's data structures, before a SphereCast will hit the collider at it's new position.

See Also: Physics.SphereCastAll, Physics.CapsuleCast, Physics.Raycast, Rigidbody.SweepTest.

#pragma strict
var charCtrl: CharacterController;
function Start() {
	charCtrl = GetComponent.<CharacterController>();
}
function Update() {
	var hit: RaycastHit;
	var p1: Vector3 = transform.position + charCtrl.center;
	var distanceToObstacle: float = 0;
	// to see if it is about to hit anything.
	if (Physics.SphereCast(p1, charCtrl.height / 2, transform.forward, hit, 10))
		distanceToObstacle = hit.distance;
}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { CharacterController charCtrl; void Start() { charCtrl = GetComponent<CharacterController>(); } void Update() { RaycastHit hit;

Vector3 p1 = transform.position + charCtrl.center; float distanceToObstacle = 0; // Cast a sphere wrapping character controller 10 meters forward // to see if it is about to hit anything. if (Physics.SphereCast(p1, charCtrl.height / 2, transform.forward, out hit, 10)) distanceToObstacle = hit.distance; } }

public static function SphereCast(ray: Ray, radius: float, maxDistance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers): bool;
public static bool SphereCast(Ray ray, float radius, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers);
public static function SphereCast(ray: Ray, radius: float, out hitInfo: RaycastHit, maxDistance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers): bool;
public static bool SphereCast(Ray ray, float radius, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers);

Parameters

ray The starting point and direction of the ray into which the sphere sweep is cast.
radius The radius of the sphere.
hitInfo If true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).
distance The length of the sweep.
layerMask A Layer mask that is used to selectively ignore colliders when casting a capsule.

Returns

bool True when the sphere sweep intersects any collider, otherwise false.

Description

Casts a sphere along a ray and returns detailed information on what was hit.

This is useful when a Raycast does not give enough precision, because you want to find out if an object of a specific size, such as a character, will be able to move somewhere without colliding with anything on the way. Think of the sphere cast like a thick raycast.

Note: The sphere cast does not work against colliders configured as triggers. If you move colliders from scripting or by animation, there needs to be at least one FixedUpdate executed so that the physics library can update it's data structures, before a SphereCast will hit the collider at it's new position.

See Also: Physics.SphereCastAll, Physics.CapsuleCast, Physics.Raycast, Rigidbody.SweepTest.