【unity2D】API-学习记录13-Physics2D.CircleCast和Physics2D.OverlapCircle

前言

最近在写敌人的行动逻辑,其中涉及到“判定玩家是否在攻击范围内”的问题,现在学习这两种方法,以期解决该问题。

API对Physics2D.CircleCast的(部分)说明

public static RaycastHit2D CircleCast(Vector2 origin, float radius, Vector2 direction, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity

A CircleCast is conceptually like dragging a circle through the Scene in a particular direction. Any object making contact with the circle can be detected and reported.
从概念上说,CircleCast 就像朝特定方向拖动一个圆形穿过场景一样。在该过程中,可以检测并报告与圆形接触的任何对象。

最重要的概念是“CircleCast 就像朝特定方向拖动一个圆形穿过场景一样”,而这个圆的半径由我们传入的radius来确定。其它的细节则与RayCast几乎无异。

API对Physics2D.OverlapCircle的(部分)说明

public static Collider2D OverlapCircle(Vector2 point, float radius, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity);

返回值
Collider2D The Collider overlapping the circle.
Collider2D 与该圆形重叠的碰撞体。

描述
The circle is defined by its centre coordinate in world space and by its radius. 
圆形由其在世界空间中的中心坐标及其半径定义。

The optional layerMask allows the test to check only for objects on specific layers.
可选的 layerMask 可让测试仅检查特定层上的对象。

Although the Z axis is not relevant for rendering or collisions in 2D, you can use the minDepth and maxDepth parameters to filter objects based on their Z coordinate.
虽然 Z 轴与 2D 中的渲染或碰撞无关,但您可以使用 minDepth 和 maxDepth 参数根据其 Z 轴坐标筛选对象。

If more than one Collider falls within the circle then the one returned will be the one with the lowest Z coordinate value.
如果有多个碰撞体位于该圆形内,则返回 Z 坐标值最小的碰撞体。

Null is returned if there are no Colliders in the circle. 
如果该圆形内没有任何碰撞体,则返回 Null。

总结

Physics2D.CircleCast Physics2D.OverlapCircle
返回值 RaycastHit2D Collider2D
投射方式 就像朝特定方向拖动一个圆形穿过场景 在世界空间中投射圆形
检测条件 与圆形接触的第一个碰撞体 在该圆形区域内的,z坐标最小的碰撞体
若未检测到 RaycastHit2D.collider = null Collider2D = null

补充

在API中,还能查阅到一些“和这两个方法分别相似”的方法,比如:
BoxCast、CapsuleCast、CircleCastAll;OverlapBox、OverlapCapsule、OverlapCircleAll等。它们和这两个方法有很多相同的地方,也有一些差异,具体如何去查阅API,不再一一介绍。

参考资料

Unity API CircleCast(EN)
Unity API CircleCast(CN)
Unity API OverlapCircle(EN)
Unity API OverlapCircle(CN)

posted @ 2021-06-03 11:44  AshScops  阅读(1982)  评论(1编辑  收藏  举报