一、敌人检测玩家
首先,我们可以使用这个Physics2D这个类来进行侦测玩家
Physics2D他提供了很多模拟2D物理行为的方法~
代码如下:
// 观察距离的偏移量——x轴
protected float watchOffsetX;
// 观察距离的偏移量——y轴
protected float watchOffsetY;
// 观察距离的区域大小
protected Vector2 watchDistance;
// 当前物体的位置
protected Vector2 watchPosition;
protected override Collider2D WatchBox()
{
// 关于检测转向 当物体的x轴 > 0 代表右边 偏移量设置正值
watchOffsetX = transform.localScale.x > 0 ? Mathf.Abs(watchOffsetX) : -Mathf.Abs(watchOffsetX);
// enemy位置
watchPosition = transform.position;
// 检测盒子偏移量设置
watchPosition.x += watchOffsetX;
watchPosition.y += watchOffsetY;
// 检测在指定区域内是否存在其他碰撞体
return Physics2D.OverlapBox(watchPosition, watchDistance, 0, playerLayer);
}
注