以下为机器人的寻路追踪巡逻代码,机器人监测在不同时候做不同的事。
void Update() {
if (robot.isinsight==true&&playerhealth.health>=0)//如果在视野内,停下追踪,开始开枪,更改动画。
{
navagent.SetDestination(transform.position);
Animator.SetBool("seeplayer", true);
Shootblood();
}
else
{
Animator.SetBool("seeplayer", false);
if (robot.alertposition != Vector3.zero&& playerhealth.health >= 0)//如果警报位置更改,开始追踪,去查看此位置,加快速度。
{
navagent.speed = 4.5f;
Chase();
}
else//巡逻时,降低速度,关闭警报。
{
Gamecontrol.getinstance.alarmOn = false;
navagent.speed = 2.5f;
Xunluo();
}
}
}
public void Xunluo()
{
if(navagent.remainingDistance<1f/*&&Vector3.Distance(this.transform.position, waypoints[index].position)<0.5*/)//巡逻时即将到达位置时,停下来记时间,时间到了,更改下一个点。
{
timer += Time.deltaTime;
if (timer > time)
{
index++;//更改下一个目标点。
index = index % 4;//满四归零
navagent.destination = waypoints[index].position;
//navagent.updatePosition = false;
//navagent.updateRotation = false;//可以设置使nav与实物分离,可能版本不同,不能实现,遂妥协。
timer = 0;//归零时间
}
}
}
public void Chase()
{
navagent.speed = 3.5f;
navagent.destination = robot.alertposition;//将追踪终点更改为警报位置
if(navagent.remainingDistance<3f)
{
print("test");
timer2 += Time.deltaTime;//此处开始计时,时间到了3f,若警报位置没有更换函数没有重复执行,说明player已经逃脱,遂关闭Chase(),开始巡逻;
if (timer2>3f)
{
robot.alertposition = Vector3.zero;
Gamecontrol.getinstance.lastPlayerposition = Vector3.zero;
}
}
}
public void Shootblood()
{
if (Animator.GetFloat("Shot") >=0.8)
{
piu.Play();
print("11");
playerhealth.health -= 100 - Vector3.Distance(this.transform.position, playerhealth.transform.position) * 8;//根据距离来计算伤害。
}
}
}