Physics.CheckSphere

检测是否有任何碰撞体重叠在定义的球体

定义

public static bool CheckSphere(
    Vector3 position,
    float radius,
    int layerMask = DefaultRaycastLayers,
    QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal
);
参数名类型必要说明
positionVector3球体中心坐标
radiusfloat球体半径
layerMaskint层级蒙版,设定后只检测此层级中的物体是否相交
queryTriggerInteractionQueryTriggerInteraction指定该查询是否触发触发器,默认 QueryTriggerInteraction.UseGlobal
QueryTriggerInteraction.UseGlobal:使用 Physics.queriesHitTriggers 设置
QueryTriggerInteraction.Ignore:忽略
QueryTriggerInteraction.Colide:触发

示例

using UnityEngine;

[RequireComponent(typeof(AudioSource))]
public class ExampleScript : MonoBehaviour
{
    private AudioSource _audioSource;
    private void Start()
    {
        _audioSource = GetComponent<AudioSource>();
    }

    private void Update()
    {

        if (Physics.CheckSphere(transform.position, 3.0f))
        {
            _audioSource.Play();
        }
    }
}