2D游戏中 敌人移动到玩家面前
//放到循环里
public
override
void
Move(
Transform
player,
Transform
npc)
{
//旋转方向
LooakAtPlayer(npc,player);
//移动
float
dist = GetDis(player,npc);
float
y = 0;
float
x = 0;
if
( !isArriveDisIn(player,npc))
{
//先x轴移动
float
xValue =
Mathf
.Abs((npc.transform.localPosition.x-player.transform.localPosition.x));
if
(xValue <= date.arriveDistanceX)
//是否接近
{
x = 0;
}
else
//距离玩家远
{
x = 1;
}
//在Y轴移动
float
yValue =
Mathf
.Abs((npc.transform.localPosition.y - player.transform.localPosition.y));
if
(yValue < date.arriveDistanceY)
//在一行
{
y = 0f;
}
else
//不和玩家在一行
{
//玩家在我的上还是下呢
if
(npc.transform.localPosition.y > player.transform.localPosition.y)
//我在玩家上面
{
y = -1f;
}
else
//我在玩家下面
{
y = 1f;
}
}
// Debug.LogError(xValue+ " =xValue,yValue= " + yValue);
Vector3
dir =
new
Vector3
(x, y, 0) *
Time
.deltaTime * curSpeed;
npc.transform.Translate(dir);
}