public static RaycastHit2D[] RaycastAll (Vector2 origin, Vector2 direction, float distance= Mathf.Infinity, int layerMask= DefaultRaycastLayers, float minDepth= -Mathf.Infinity, float maxDepth= Mathf.Infinity);

Parameters

origin射线在 2D 空间中的起点。
directionA vector representing the direction of the ray.
distanceThe maximum distance over which to cast the ray.
layerMask过滤器,用于仅在特定层上检测碰撞体。
minDepth仅包括 Z 坐标(深度)大于或等于该值的对象。
maxDepth仅包括 Z 坐标(深度)小于或等于该值的对象。

Returns

RaycastHit2D[] 返回的投射数量。

Description

Casts a ray against colliders in the Scene, returning all colliders that contact with it.

从概念上说,射线投射 类似于从空间中的某个点朝特定方向发射一条光束。在该过程中,可以检测并报告与光束接触的任何对象。

该函数类似于 Raycast 函数,但它并非仅检测命中的第一个碰撞体,而是返回一个射线行进路径上接触到的所有碰撞体的数组。数组中的碰撞体按到原点的距离顺序排序。layerMask 可用于仅在特定层上有选择地检测对象(例如,这让您能够仅将检测应用于敌人角色)。

对于确定视线、炮火击中的目标以及游戏中的许多其他目的来说,射线投射很有用。

Additionally, this will also detect Collider(s) at the start of the ray. In this case, the ray starts inside the Collider and doesn't intersect the Collider surface. This means that the collision normal cannot be calculated, in which case the returned collision normal 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.

另请参阅:LayerMask 类、RaycastHit2D 类、RaycastLinecastDefaultRaycastLayersIgnoreRaycastLayerraycastsHitTriggers