public int Raycast (Vector2 direction, RaycastHit2D[] results, float distance= Mathf.Infinity, int layerMask= Physics2D.AllLayers, float minDepth= -Mathf.Infinity, float maxDepth= Mathf.Infinity);
public int Raycast (Vector2 direction, ContactFilter2D contactFilter, RaycastHit2D[] results, float distance= Mathf.Infinity);

Parameters

direction表示光线方向的向量。
results用于接收结果的数组。
distance光线的最大投射距离。
layerMask筛选器,用于检查仅在指定层上的对象。
minDepth仅包括 Z 坐标(深度)大于该值的对象。
maxDepth仅包括 Z 坐标(深度)小于该值的对象。
contactFilter筛选由接触筛选器定义的结果。

Returns

int 返回的结果数量。

Description

Casts a ray into the Scene that starts at the Collider position and ignores the Collider itself.

此函数类似于 [[Physics2D::RaycastNonAlloc]] 函数,同样,这些结果返回在提供的数组中。整数返回值是为与该光线交叠的对象数(可能为零),但如果结果数组中没有足够的元素来报告所有这些结果,则不会调整该数组的大小。其意义在于不为这些结果分配内存,因此,如果经常进行光线投射,可以提高垃圾回收性能。

使用 contactFilter 筛选器的此函数重载按 ContactFilter2D 中提供的选项筛选结果。

此外,这还将检测位于光线起点的其他碰撞体。在这种情况下,光线从碰撞体内部开始,并且不与碰撞体表面交叠。这意味着无法计算碰撞法线,在这种情况下,返回的碰撞法线设置为正在测试的光线向量的倒数。这可轻松检测到,因为此类结果始终是 RaycastHit2D 分数为零。

另请参阅:LayerMask 类、RaycastHit2D 类、[[Physics2D::RaycastNonAlloc]]、[[Physics2D::AllLayers]]、[[Physics2D::IgnoreRaycastLayer]]、[[Physics2D::raycastsHitTriggers]]。


Parameters

direction表示光线方向的向量。
contactFilter筛选由接触筛选器定义的结果。
resultsList to receive results.
distance光线的最大投射距离。

Returns

void 返回的结果数量。

Description

Casts a ray into the Scene that starts at the Collider position and ignores the Collider itself.

This function is similar to the [[Physics2D::RaycastNonAlloc]] function and in the same way, the results are returned in the supplied list. The integer return value is the number of Colliders that intersect the ray (possibly zero). The results list will be resized if it doesn't contain enough elements to report all the results. This prevents memory from being allocated for results when the results list does not need to be resized, and improves garbage collection performance when the query is performed frequently.

Additionally, this will also detect other Collider(s) at the start of the ray. In this case the ray is starting inside the Collider and doesn't intersect the Collider surface. This means that the collision normal cannot be calculated in which case the collision normal returned is set to the inverse of the ray vector being tested. This can easily be detected because such results are always at a RaycastHit2D fraction of zero.

See Also: RaycastHit2D class.