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; void Start() { //抓取代理組件 agent1 = this.GetComponent(); } bool iscollide = false; Vector3 prev_position = new Vector3(0,0,0); private void OnTriggerEnter(Collider other) { if(other.name == "door3" && iscollide==false) { iscollide = true; //把對方cube(3)加入一個子物件(agent) //正確做法: this.transform.SetParent(other.transform); //錯誤做法: //this.transform.position = other.transform.position; //播放過關音效 other.GetComponent().Play(); //顯示過關畫面 img_Win.SetActive(true); } //撞到第一個門,顯示gameover_screen else if(other.name == "door1" || other.name == "door2") { //播放過關失敗音效 other.GetComponent().Play(); img_Over.SetActive(true); } } void Update() { //手觸控某個位置,agent1就會朝點的位置走過去 //判別按下滑鼠左鍵 bool isjump = false; if (Input.GetMouseButtonDown(0)) { //射線取得點按位置的坐標 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") { //設定主角jump動畫(但是沒有效果),可能是update馬上被改成status=1 GameObject.Find("unitychan").GetComponent().SetInteger("status", 2); //所以用一個isjump變數 isjump = true; } else { GameObject.Find("unitychan").GetComponent().SetInteger("status", 1); } } } //注意:要先判斷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; } }