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

Parameters

direction表示形状投射方向的向量。
results用于接收结果的数组。
distance将形状投射的最大距离。
ignoreSiblingCollidersDetermines whether the cast should ignore Colliders attached to the same Rigidbody2D (known as sibling Colliders).

Returns

int 返回的结果数量。

Description

Casts the Collider shape into the Scene starting at the Collider position ignoring the Collider itself.

This function will take the collider shape and cast it into the Scene starting at the collider position in the specified direction for an optional distance and return the results in the provided results array. The integer return value is the number of results written into the results array. The results array will not be resized if it doesn't contain enough elements to report all the results. The significance of this is that no memory is allocated for the results and so garbage collection performance is improved when casts are performed frequently.

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

Parameters

direction表示形状投射方向的向量。
contactFilter筛选由接触筛选器定义的结果。
results用于接收结果的数组。
distance将形状投射的最大距离。
ignoreSiblingCollidersDetermines whether the cast should ignore Colliders attached to the same Rigidbody2D (known as sibling Colliders).

Returns

int 返回的结果数量。

Description

Casts the Collider shape into the Scene starting at the Collider position ignoring the Collider itself.

This function will take the collider shape and cast it into the Scene starting at the collider position in the specified direction for an optional distance and return the results in the provided results array. The integer return value is the number of results written into the results array. The results array will not be resized if it doesn't contain enough elements to report all the results. The significance of this is that no memory is allocated for the results and so garbage collection performance is improved when casts are performed frequently.

The contactFilter parameter can filter the returned results by the options in ContactFilter2D.

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


Parameters

direction表示形状投射方向的向量。
contactFilter筛选由接触筛选器定义的结果。
resultsList to receive results.
distance将形状投射的最大距离。
ignoreSiblingCollidersDetermines whether the cast should ignore Colliders attached to the same Rigidbody2D (known as sibling Colliders).

Returns

void 返回的结果数量。

Description

Casts the Collider shape into the Scene starting at the Collider position ignoring the Collider itself.

This function will take the Collider shape and cast it into the Scene starting at the Collider position in the specified direction 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.

The contactFilter parameter can filter the returned results by the options in ContactFilter2D.

Additionally, this also detects other Collider(s) at the Collider start position if they are overlapping. In this case, the cast shape will start inside the Collider and may not 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 direction vector being tested.