Version: 5.4
public static bool SphereCast (Vector3 origin, float radius, Vector3 direction, out RaycastHit hitInfo, float maxDistance= Mathf.Infinity, int layerMask= DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction= QueryTriggerInteraction.UseGlobal);

Parámetros

origin El centro de la esfera al principio del barrido.
radius The radius of the sphere.
direction La dirección en la cual barrer la esfera.
hitInfo Si true se devuelve, hitInfo contendrá más información acerca de dónde el collider ha golpeado (See Also: RaycastHit).
maxDistance La longitud máxima de la emisión.
layerMask Una Layer mask que es utilizada para ignorar colliders selectivamente cuando se emita una cápsula.
queryTriggerInteraction Especifica si esta consulta debería golpear Triggers.

Valor de retorno

bool True cuando el barrido de la esfera intersecta cualquier collider, de lo contrario false.

Descripción

Emite una esfera a lo largo de un rayo y retorna información detallada sobre qué fue hit (golpeado).

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.
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.

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 bool SphereCast (Ray ray, float radius, float maxDistance= Mathf.Infinity, int layerMask= DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction= QueryTriggerInteraction.UseGlobal);

Parámetros

ray El punto inicial y dirección del rayo en la que se ejecuta el barrido de la esfera.
radius The radius of the sphere.
maxDistance La longitud máxima de la emisión.
layerMask Una Layer mask que es utilizada para ignorar colliders selectivamente cuando se emita una cápsula.
queryTriggerInteraction Especifica si esta consulta debería golpear Triggers.

Valor de retorno

bool True cuando el barrido de la esfera intersecta cualquier collider, de lo contrario false.

Descripción

Emite una esfera a lo largo de un rayo y retorna información detallada sobre qué fue hit (golpeado).

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.

Nota: Si usted mueve colliders desde scripting o por animación, necesita haber al menos un FixedUpdate ejecutado para que la librería de física pueda actualizar sus estructuras de datos, antes de que un SphereCast golpeé el collider en su nueva posición.

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


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

Parámetros

ray El punto inicial y dirección del rayo en la que se ejecuta el barrido de la esfera.
radius The radius of the sphere.
hitInfo Si true se devuelve, hitInfo contendrá más información acerca de dónde el collider ha golpeado (See Also: RaycastHit).
maxDistance La longitud máxima de la emisión.
layerMask Una Layer mask que es utilizada para ignorar colliders selectivamente cuando se emita una cápsula.
queryTriggerInteraction Especifica si esta consulta debería golpear Triggers.

Descripción