1. 程式人生 > >關於機器人尋路追蹤巡邏的一些

關於機器人尋路追蹤巡邏的一些

以下為機器人的尋路追蹤巡邏程式碼,機器人監測在不同時候做不同的事。

  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;//根據距離來計算傷害。
            }
        }
     }