using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; using UnityEngine.UI; public class enemy_greenp_manager : MonoBehaviour { NavMeshAgent agentChan, agentGreen; //注意:somke_prefab要從asset拖曵prefabs進來,不能由場景內拖曵隱藏的somke_prefab(會造成還要設定setActive(true)) [Tooltip("敵方被KO的煙霧動畫prefab")] public GameObject somke_prefab; [Tooltip("打到主角O的動畫prefab")] public GameObject hurted_prefab; private GameObject node_smoke; private GameObject node_hurtAnimation; //攻擊冷卻時間(攻擊一次,必須再等幾秒才能再攻擊) public float lastattackTime = 2.0f; //體力 public int energy = 100; //GameOver的image public GameObject img_Over; //判別是否死亡 bool isDie = false; //錯誤做法:不可以在前台,拖曵TextEnergy成一個變數,否則當產生怪物prefabs時,會怪物與主角碰撞扣分後,會當掉 //Text //public Text textEnergy; void Start() { agentChan = GameObject.Find("unitychan").GetComponent(); agentGreen = this.GetComponent(); GameObject.Find("Text_energy").transform.position = new Vector2(300, 100); } // Update is called once per frame void Update() { ////隨時判別角色狀態 //SwitchStatus(); //攻擊冷卻時間-Time.deltaTime lastattackTime -= Time.deltaTime; //如果status=4,躺下 if (this.gameObject.GetComponent().GetInteger("status") == 4) { //this.gameObject.GetComponent().SetInteger("status", 4); //死亡後,不再追踪 //方法1:把原本追逐主角(SetDestination),改成追踪自己的位置 //agentGreen.SetDestination(agentGreen.transform.position); //方法2:把navMeshAgent.Stop() //agentGreen.Stop(); //方法3:不設定,就是還是繼續追踪主角 //15秒後消失 Invoke("removeObject", 3.0f); } else if(isDie == false)//其它都是走路 status=1,追踪主角 { agentGreen.SetDestination(agentChan.transform.position); agentGreen.GetComponent().SetInteger("status", 1); } //計算enemy與主角unityChan的距離,靠近就刺 float distance1 = Vector3.Distance(agentChan.transform.position, agentGreen.transform.position); if (lastattackTime < 0 && distance1 < 2.5f && isDie == false) //缺點:在udapte裡面若是設定距離小2.5就攻擊,那麼1秒內會連續攻擊60次 //改善:宣告一個lastattackTime變數,超過這個時間再攻擊第二次 { //靠近主角,就刺 status = 14(或12) this.gameObject.GetComponent().SetInteger("status", 14); //降低主角的體力 //energy -= 3; //顯示分數在畫面Text_energy //GameObject.Find("Text_energy").GetComponent().text = "體力:" + energy.ToString(); //GameObject.Find("Text_energy").transform.position = new Vector2(100, 50); //show_energy(-3); //若是多個怪物prefabs,改成呼叫score.cs裡面的showScore() GameObject.FindObjectOfType().showScore(-3); //產生被打的動畫prefabs node_hurtAnimation = Instantiate(hurted_prefab, null); node_hurtAnimation.transform.position = GameObject.Find("unitychan").transform.position; //如果體力<0,則過關失敗 if (energy < 0) { //播放過關失敗音效 GameObject.Find("door2").GetComponent().Play(); img_Over.SetActive(true); } //播放hit/punch音效 this.GetComponent().Play(); //重新設定lastattackTime = 0.5f lastattackTime = 2.0f; } } private void OnTriggerEnter(Collider other) { if (other.name == "door2" || other.name == "door1" || other.name == "door3" || other.name == "Bomb" && isDie == false) { //顯示煙霧動畫,放在根目錄,但是只有出現2秒(粒子系統只設定2秒),粒子會自動消失,不需要刪除 node_smoke = Instantiate(somke_prefab, null); //以下做法,都不需要 //node_smoke.SetActive(true); //Invoke("show_smoke", 2); //設定動畫顯示的坐標 node_smoke.transform.position = this.transform.position; //播放音效 node_smoke.GetComponent().Play(); //設定animator的status=4,躺下 this.gameObject.GetComponent().SetInteger("status", 4); //死亡 isDie = true; }//如果碰到主角,則朝主角刺(結果,無法設定,因為,enemy會朝向unityChan靠近,直到距離=0,所以最後不會碰撞 //else if(other.name == "unitychan" && this.gameObject.GetComponent().GetInteger("status") != 4) else if (other.name == "unitychan" && isDie == false) { this.gameObject.GetComponent().SetInteger("status", 14); //14或12 } } private void removeObject() { Destroy(this.gameObject); }