Version: 2023.2
public int Cast (Vector2 direction, List<RaycastHit2D> results, float distance= Mathf.Infinity);

参数

direction 表示每个 Collider2D 形状投射方向的矢量。
results 用于接收结果的列表。
distance 将碰撞体投射的最大距离。

返回

int 返回放置在 results 列表中的结果数。

描述

附加到 Rigidbody2D 的所有 Collider2D 形状都将投射到场景中 - 从每个碰撞体的位置开始,忽略附加到同一个 Rigidbody2D 的碰撞体。

此函数接受附加到 Rigidbody2D 的所有 Collider2D 形状,并将其投射到从指定 direction 上的碰撞体位置开始的可选 distance 的场景中,并在提供的 results 列表中返回结果。

该整数返回值是写入到 results 列表中的结果数。如果结果列表中没有足够的元素来报告所有这些结果,则会调整列表的大小。这可在 results 列表无需调整大小时防止为结果分配内存,可在经常执行查询时提高垃圾收集性能。

此外,这还将检测位于碰撞体开始位置的其他碰撞体(如果它们重叠)。在这种情况下,投射形状将从碰撞体内部开始,可能不与碰撞体表面交叠。这意味着无法计算碰撞法线,在这种情况下,返回的碰撞法线设置为正在测试的 direction 向量的倒数。


public int Cast (Vector2 direction, ContactFilter2D contactFilter, List<RaycastHit2D> results, float distance= Mathf.Infinity);

参数

direction 表示每个 Collider2D 形状投射方向的矢量。
contactFilter 筛选由接触筛选器定义的结果。
results 用于接收结果的列表。
distance 将碰撞体投射的最大距离。

返回

int 返回放置在 results 列表中的结果数。

描述

附加到 Rigidbody2D 的所有 Collider2D 形状都将投射到场景中 - 从每个碰撞体的位置开始,忽略附加到同一个 Rigidbody2D 的碰撞体。

此函数接受附加到 Rigidbody2D 的所有 Collider2D 形状,并将其投射到从指定 direction 上的碰撞体位置开始的可选 distance 的场景中,并在提供的 results 列表中返回结果。

该整数返回值是写入到 results 列表中的结果数。如果结果列表中没有足够的元素来报告所有这些结果,则会调整列表的大小。这可在 results 列表无需调整大小时防止为结果分配内存,可在经常执行查询时提高垃圾收集性能。

contactFilter 参数可按 ContactFilter2D 中的选项筛选返回的结果。

此外,这还将检测位于碰撞体开始位置的其他碰撞体(如果它们重叠)。在这种情况下,投射形状将从碰撞体内部开始,可能不与碰撞体表面交叠。这意味着无法计算碰撞法线,在这种情况下,返回的碰撞法线设置为正在测试的 direction 向量的倒数。


public int Cast (Vector2 direction, RaycastHit2D[] results, float distance= Mathf.Infinity);

参数

direction 表示每个 Collider2D 形状投射方向的矢量。
results 用于接收结果的数组。
distance 将碰撞体投射的最大距离。

返回

int 返回放置在 results 数组中的结果数。

描述

附加到 Rigidbody2D 的所有 Collider2D 形状都将投射到场景中 - 从每个碰撞体的位置开始,忽略附加到同一个 Rigidbody2D 的碰撞体。

此函数接受附加到 Rigidbody2D 的所有 Collider2D 形状,并将其投射到从指定 direction 上的碰撞体位置开始的可选 distance 的场景中,并在提供的 results 数组中返回结果。

该整数返回值是写入到 results 数组中的结果数。如果结果数组中没有足够的元素来报告所有这些结果,则不会调整数组的大小。其意义在于不为这些结果分配内存,因此在经常进行投射时会提高垃圾收集性能。

此外,这还将检测位于碰撞体开始位置的其他碰撞体(如果它们重叠)。在这种情况下,投射形状将从碰撞体内部开始,可能不与碰撞体表面交叠。这意味着无法计算碰撞法线,在这种情况下,返回的碰撞法线设置为正在测试的 direction 向量的倒数。


public int Cast (Vector2 direction, ContactFilter2D contactFilter, RaycastHit2D[] results, float distance= Mathf.Infinity);

参数

direction 表示每个 Collider2D 形状投射方向的矢量。
contactFilter 筛选由接触筛选器定义的结果。
results 用于接收结果的数组。
distance 将碰撞体投射的最大距离。

返回

int 返回放置在 results 数组中的结果数。

描述

附加到 Rigidbody2D 的所有 Collider2D 形状都将投射到场景中 - 从每个碰撞体的位置开始,忽略附加到同一个 Rigidbody2D 的碰撞体。

此函数接受附加到 Rigidbody2D 的所有 Collider2D 形状,并将其投射到从指定 direction 上的碰撞体位置开始的可选 distance 的场景中,并在提供的 results 数组中返回结果。

该整数返回值是写入到 results 数组中的结果数。如果结果数组中没有足够的元素来报告所有这些结果,则不会调整数组的大小。其意义在于不为这些结果分配内存,因此在经常进行投射时会提高垃圾收集性能。

contactFilter 参数可按 ContactFilter2D 中的选项筛选返回的结果。

此外,这还将检测位于碰撞体开始位置的其他碰撞体(如果它们重叠)。在这种情况下,投射形状将从碰撞体内部开始,可能不与碰撞体表面交叠。这意味着无法计算碰撞法线,在这种情况下,返回的碰撞法线设置为正在测试的 direction 向量的倒数。


public int Cast (Vector2 position, float angle, Vector2 direction, List<RaycastHit2D> results, float distance= Mathf.Infinity);

参数

position The position to start casting the rigidbody from.
angle 刚体的旋转。
direction 表示每个 Collider2D 形状投射方向的矢量。
results 用于接收结果的列表。
distance 将碰撞体投射的最大距离。

返回

int 返回放置在 results 列表中的结果数。

描述

The Rigidbody2D is cast into the Scene starting at the specified position and rotation. All the Collider2D shapes attached to the Rigidbody2D are cast along with it ignoring the Colliders attached to the same Rigidbody2D.

This function, unlike Rigidbody2D.Cast allows you to cast the Rigidbody2D and all its attached Collider2D through the scene but also allows the starting position and angle of the Rigidbody2D to be specified.

该整数返回值是写入到 results 列表中的结果数。如果结果列表中没有足够的元素来报告所有这些结果,则会调整列表的大小。这可在 results 列表无需调整大小时防止为结果分配内存,可在经常执行查询时提高垃圾收集性能。

NOTE: Additionally, this will also detect other Collider(s) overlapping the start position. In this case the cast shape will be starting inside another Collider and may not 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 direction vector being tested.

Additional resources: Rigidbody2D.Cast and ContactFilter2D.


public int Cast (Vector2 position, float angle, Vector2 direction, ContactFilter2D contactFilter, List<RaycastHit2D> results, float distance= Mathf.Infinity);

参数

position The position to start casting the rigidbody from.
angle 刚体的旋转。
direction 表示每个 Collider2D 形状投射方向的矢量。
contactFilter 筛选由接触筛选器定义的结果。
results 用于接收结果的列表。
distance 将碰撞体投射的最大距离。

返回

int 返回放置在 results 列表中的结果数。

描述

The Rigidbody2D is cast into the Scene starting at the specified position and rotation. All the Collider2D shapes attached to the Rigidbody2D are cast along with it ignoring the Colliders attached to the same Rigidbody2D.

This function, unlike Rigidbody2D.Cast allows you to cast the Rigidbody2D and all its attached Collider2D through the scene but also allows the starting position and angle of the Rigidbody2D to be specified.

This function also uses the specified contactFilter therefore the specific LayerMask of each Collider2D attached to this Rigidbody2D is not used to detect contacts. If this isn't desired then the CastFrom overload without the ContactFilter2D argument should be used.

该整数返回值是写入到 results 列表中的结果数。如果结果列表中没有足够的元素来报告所有这些结果,则会调整列表的大小。这可在 results 列表无需调整大小时防止为结果分配内存,可在经常执行查询时提高垃圾收集性能。

NOTE: Additionally, this will also detect other Collider(s) overlapping the start position. In this case the cast shape will be starting inside another Collider and may not 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 direction vector being tested.

Additional resources: Rigidbody2D.Cast and ContactFilter2D.