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

参数

direction 表示形状投射方向的向量。
results 用于接收结果的列表。
distance 将形状投射的最大距离。
ignoreSiblingColliders 确定投射是否应忽略附加到同一 Rigidbody2D 的碰撞体(称为同级碰撞体)。

返回

int 返回的结果数量。

描述

将碰撞体形状投射到从该碰撞体位置开始的场景中,而忽略该碰撞体本身。

此函数接受碰撞体形状,并将其投射到从指定 direction 上的碰撞体位置开始的可选 distance 的场景中,并在提供的 results 列表中返回结果。该整数返回值是写入到 results 列表中的结果数。如果结果列表中没有足够的元素来报告所有这些结果,则会调整列表的大小。这可在 results 列表无需调整大小时防止为结果分配内存,可在经常执行投射时提高垃圾收集性能。

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


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

参数

direction 表示形状投射方向的向量。
contactFilter 筛选由接触筛选器定义的结果。
results 用于接收结果的列表。
distance 将形状投射的最大距离。
ignoreSiblingColliders 确定投射是否应忽略附加到同一 Rigidbody2D 的碰撞体(称为同级碰撞体)。

返回

int 返回的结果数量。

描述

将碰撞体形状投射到从该碰撞体位置开始的场景中,而忽略该碰撞体本身。

此函数接受碰撞体形状,并将其投射到从指定 direction 上的碰撞体位置开始的可选 distance 的场景中,并在提供的 results 列表中返回结果。该整数返回值是写入到 results 列表中的结果数。如果结果列表中没有足够的元素来报告所有这些结果,则会调整列表的大小。这可在 results 列表无需调整大小时防止为结果分配内存,可在经常执行投射时提高垃圾收集性能。

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

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


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

参数

direction 表示形状投射方向的向量。
results 用于接收结果的数组。
distance 将形状投射的最大距离。
ignoreSiblingColliders 确定投射是否应忽略附加到同一 Rigidbody2D 的碰撞体(称为同级碰撞体)。

返回

int 返回的结果数量。

描述

将碰撞体形状投射到从该碰撞体位置开始的场景中,而忽略该碰撞体本身。

此函数接受碰撞体形状,并将其投射到从指定 direction 上的碰撞体位置开始的可选 distance 的场景中,并在提供的 results 数组中返回结果。该整数返回值为写入到 results 数组中的结果数。如果结果数组中没有足够的元素来报告所有这些结果,则不会调整数组的大小。其意义在于不为这些结果分配内存,因此在经常进行投射时会提高垃圾收集性能。

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

**注意:**使用 Collider2D.Cast() 需要使用 Rigidbody2D。如果 未声明 Rigidbody2D,则 Cast() 不起作用。但 Rigidbody2D 可以是静态的,也可以附加到 Collider2D。这将使 Cast() 正常 运行。此外,如果 Collider2D 对象没有 Rigidbody2D 对象, 则它可与同时具有 Collider2DRigidbody2D 对象的对象碰撞。


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

参数

direction 表示形状投射方向的向量。
contactFilter 筛选由接触筛选器定义的结果。
results 用于接收结果的数组。
distance 将形状投射的最大距离。
ignoreSiblingColliders 确定投射是否应忽略附加到同一 Rigidbody2D 的碰撞体(称为同级碰撞体)。

返回

int 返回的结果数量。

描述

将碰撞体形状投射到从该碰撞体位置开始的场景中,而忽略该碰撞体本身。

此函数接受碰撞体形状,并将其投射到从指定 direction 上的碰撞体位置开始的可选 distance 的场景中,并在提供的 results 数组中返回结果。该整数返回值为写入到 results 数组中的结果数。如果结果数组中没有足够的元素来报告所有这些结果,则不会调整数组的大小。其意义在于不为这些结果分配内存,因此在经常进行投射时会提高垃圾收集性能。

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

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


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

参数

position The position to start casting the Collider from.
angle The rotation of the Collider.
direction Vector representing the direction to cast the Collider.
results 用于接收结果的列表。
distance Maximum distance over which to cast the Collider.
ignoreSiblingColliders Determines whether the cast should ignore other Colliders attached to the same Rigidbody2D (known as sibling colliders).

返回

int 返回的结果数量。

描述

Casts the Collider shape into the Scene starting at the specified position and rotation.

This function will take the Collider shape and cast it into the Scene starting at the specified position and angle for an optional distance and return the results in the provided results list.

The integer return value is the number of results written into the results list. 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 casts are performed frequently.

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

NOTE: The position and angle used here represent the position of the Rigidbody2D the Collider2D is attached to. If the Collider2D is offset from the center of mass then the Collider2D will be overlapped at the same offset. This can be confusing so it is recommened that only Collider2D that align with the center of mass are used. If not then you must take this into account. If the Collider2D is not attached to a Rigidbody2D, this call cannot be used and will result in a warning.


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

参数

position The position to start casting the Collider from.
angle The rotation of the Collider.
direction Vector representing the direction to cast the Collider.
contactFilter 筛选由接触筛选器定义的结果。
results 用于接收结果的列表。
distance Maximum distance over which to cast the Collider.
ignoreSiblingColliders Determines whether the cast should ignore other Colliders attached to the same Rigidbody2D (known as sibling colliders).

返回

int 返回的结果数量。

描述

Casts the Collider shape into the Scene starting at the specified position and rotation.

This function will take the Collider shape and cast it into the Scene starting at the specified position and angle for an optional distance and return the results in the provided results list.

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

The integer return value is the number of results written into the results list. 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 casts are performed frequently.

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

NOTE: The position and angle used here represent the position of the Rigidbody2D the Collider2D is attached to. If the Collider2D is offset from the center of mass then the Collider2D will be overlapped at the same offset. This can be confusing so it is recommened that only Collider2D that align with the center of mass are used. If not then you must take this into account. If the Collider2D is not attached to a Rigidbody2D, this call cannot be used and will result in a warning.