public static bool CheckSphere (Vector3 position, float radius, int layerMask= DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction= QueryTriggerInteraction.UseGlobal);

参数

position球体的中心。
radius球体的半径。
layerMask 层遮罩,用于在投射胶囊体时有选择地忽略碰撞体。
queryTriggerInteraction指定该查询是否应该命中触发器。

描述

如果有任何碰撞体与世界坐标系中由 positionradius 界定的球体重叠,则返回 true。

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public float sphereRadius; AudioSource audioSource;

void Start() { audioSource = GetComponent<AudioSource>(); }

void WarningNoise() { // Play a noise if an object is within the sphere's radius. if (Physics.CheckSphere(transform.position, sphereRadius)) { audioSource.Play(); } } }