# Unity人工智能学习—确定性AI算法之反追踪算法

CSDN个人博客地址，凯尔八阿哥栏http://blog.csdn.net/zhangxiao13627093203，转载请注明出处

  //先判断距离
Vector2 vecDis = this.transform.position - target.transform.position;
if(PointDistance_2D(vecDis.x,vecDis.y)<MINDodgeDistance)
{
//x方向的闪避
if(target.transform.position.x<this.transform.position.x)
{
this.transform.position += new Vector3(dodge_x, 0, 0) * DodgeSpeed;
}
else if(target.transform.position.x>this.target.transform.position.x)
{
this.transform.position -= new Vector3(dodge_x, 0, 0) * DodgeSpeed;
}
//y方向的闪避
if(target.transform.position.y<this.transform.position.y)
{
this.transform.position += new Vector3(0, dodge_y, 0) * DodgeSpeed;
}
else if(target.transform.position.y>this.transform.position.y)
{
this.transform.position += new Vector3(0, dodge_y, 0) * DodgeSpeed;
}
}
//让它移动到屏幕的中间
else
{
//x方向的移动
if(Screen.width/2>this.transform.position.x)
{
this.transform.position += new Vector3(freeMove_x, 0, 0) * freeMoveSpeed;
}
else if(Screen.width/2<this.transform.position.x)
{
this.transform.position -= new Vector3(freeMove_x, 0, 0) * freeMoveSpeed;
}
//y方向上的移动
if(Screen.height/2>this.transform.position.y)
{
this.transform.position += new Vector3(0, freeMove_y, 0) * freeMoveSpeed;
}
else if(Screen.height/2<this.transform.position.y)
{
this.transform.position -= new Vector3(0, freeMove_y, 0) * freeMoveSpeed;
}
}