using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class playerControl : MonoBehaviour { private NavMeshAgent agent1; public GameObject img_Over; public GameObject img_Win; [Tooltip("讀取另外一個程式碼物件")] public enemy_greenp_manager enemy_manager_script; //[Tooltip("被door撞到的煙霧動畫prefab")] private GameObject somke_prefab_2; //大爆炸 public GameObject WFX_Nuke; private GameObject wfx1; void Start() { //抓取代理組件 agent1 = this.GetComponent(); } bool iscollide = false; bool isfire = false; Vector3 prev_position = new Vector3(0,0,0); private void OnTriggerEnter(Collider other) { //if(other.name == "Cube (1)" && iscollide==false) if (other.name == "button_fire" && isfire == false) //if (iscollide == true) { isfire = true; //爆炸畫面(顯示在根目錄null) wfx1 = Instantiate(WFX_Nuke, null); //設定爆炸畫面的位置 wfx1.transform.position = GameObject.Find("Bomb").transform.position; //把炸彈的box_collider打開,好讓炸彈碰撞怪物,但是只有打開3秒 GameObject.Find("Bomb").GetComponent().enabled = true; //3秒後,關閉炸彈的box_collider Invoke("TurnOffBoxCollider", 3); } //撞到第一個門,顯示gameover_screen else if (other.name == "door1" || other.name == "door2" || other.name == "door3") { Debug.Log("(other.name=" + other.name); //跨程式碼檔案,傳遞數值,撞到door,扣分10 GameObject.FindObjectOfType().showScore(-10); //顯示煙霧動畫,放在根目錄,但是只有出現2秒(粒子系統只設定2秒),粒子會自動消失,不需要刪除 somke_prefab_2 = Instantiate(enemy_manager_script.somke_prefab, null); somke_prefab_2.transform.position = this.transform.position; //如果體力<0,則過關失敗 if (enemy_manager_script.energy < 0) { //播放過關失敗音效 other.GetComponent().Play(); img_Over.SetActive(true); } } else { //設定button_fire為淡綠藍色 GameObject.Find("button_fire").GetComponent().material.color = new Color32(123, 202,194, 255); } } //把Bomb的BoxCollider關閉 void TurnOffBoxCollider() { GameObject.Find("Bomb").GetComponent().enabled = false; } void OnTriggerExit(Collider other) { if (other.name == "button_fire" && isfire == true) { isfire = false; } } void Update() { //手觸控某個位置,agent1就會朝點的位置走過去 //判別按下滑鼠左鍵 bool isjump = false; if (Input.GetMouseButtonDown(0)) { Debug.Log(GameObject.Find("unitychan").GetComponent().GetInteger("status")); //射線取得點按位置的坐標 Ray ray1 = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit1; //射線檢測 if(Physics.Raycast(ray1,out hit1)==true) { //取得點擊位置 Vector3 point1 = hit1.point; //設定point1為導航目標點 agent1.SetDestination(point1); //如果jump to (link2)則jump if(hit1.collider.gameObject.name=="link2" || hit1.collider.gameObject.name == "door3" || hit1.collider.gameObject.name == "Cube (3)" || hit1.collider.gameObject.name == "Cube (4)" || hit1.collider.gameObject.name == "Cube (5)" || hit1.collider.gameObject.name == "Cube" || hit1.collider.gameObject.name == "Cube (1)") { //設定主角jump動畫(但是沒有效果),可能是update馬上被改成status=1 GameObject.Find("unitychan").GetComponent().SetInteger("status", 2); //所以用一個isjump變數 isjump = true; //設定button_fire為淡綠藍色 GameObject.Find("button_fire").GetComponent().material.color = new Color32(123, 202, 194, 255); }//觸碰button_fire變成紅色 else if(hit1.collider.gameObject.name == "button_fire") { //把button_fire的顏色變紅 GameObject.Find("button_fire").GetComponent().material.SetColor("_Color", Color.red); } else { GameObject.Find("unitychan").GetComponent().SetInteger("status", 1); //設定button_fire為淡綠藍色 GameObject.Find("button_fire").GetComponent().material.color = new Color32(123, 202, 194, 255); } } } //注意:要先判斷Jump的case,否則永遠不會執行jump //jump if (isjump == true) { GameObject.Find("unitychan").GetComponent().SetInteger("status", 2); } //如果原地不動,則status =0 else if (GameObject.Find("unitychan").transform.position == prev_position) { GameObject.Find("unitychan").GetComponent().SetInteger("status", 0); } else//否則walk { GameObject.Find("unitychan").GetComponent().SetInteger("status", 1); } //設定prev_position prev_position = GameObject.Find("unitychan").transform.position; } }