Version: 2020.1

Physics.OverlapSphereNonAlloc

切换到手册
public static int OverlapSphereNonAlloc (Vector3 position, float radius, Collider[] results, int layerMask= AllLayers, QueryTriggerInteraction queryTriggerInteraction= QueryTriggerInteraction.UseGlobal);

参数

position 球体的中心。
radius 球体的半径。
results 用于存储结果的缓冲区。
layerMask Layer mask 定义要在查询中包括哪些碰撞体层。
queryTriggerInteraction 指定该查询是否应该命中触发器。

返回

int 返回存储在 results 缓冲区中的碰撞体的数量。

描述

计算与球体接触或位于球体内部的碰撞体,并将它们存储到提供的缓冲区中。

如果缓冲区空间用尽,请勿尝试增大缓冲区。当缓冲区已满时,将返回缓冲区的长度。 与 Physics.OverlapSphere 类似,但不产生任何垃圾。 另请参阅:Physics.AllLayers

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { void ExplosionDamage(Vector3 center, float radius) { int maxColliders = 10; Collider[] hitColliders = new Collider[maxColliders]; int numColliders = Physics.OverlapSphereNonAlloc(center, radius, hitColliders); for (int i = 0; i < numColliders; i++) { hitColliders[i].SendMessage("AddDamage"); } } }

另请参阅:层遮罩,特别是“选择性投射光线”,以查看“层遮罩”的详细示例。