using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class ageng_Move : MonoBehaviour { //代理人(角色) NavMeshAgent agent1; //腳踩圓圈 GameObject circle1; //圓圈材質 public Material mat1; //是否agent靠近circle bool isclose = false; void Start() { agent1 = GameObject.Find("unitychan").GetComponent(); //產生一個腳踩圓圈 circle1 = GameObject.CreatePrimitive(PrimitiveType.Cylinder); circle1.transform.localScale = new Vector3(1.5f, 0.01f, 1.5f); circle1.GetComponent().material = mat1; circle1.SetActive(false); } // Update is called once per frame void Update() { //全局偵測,是否按下滑鼠左鍵 if(Input.GetMouseButtonDown(0)) { //發出射線(滑鼠點-camera射線) Ray ray1 = Camera.main.ScreenPointToRay(Input.mousePosition); //宣告碰撞變數 RaycastHit hit1; //判別是否射線碰到物件,發生碰撞了 if (Physics.Raycast(ray1, out hit1) == true) { //顯示腳踩圓圈的位置(移動到hit1.point) //circle1.SetActive(false); //然後再顯示圓柱 circle1.SetActive(true); circle1.transform.position = hit1.point; //circle1.SetActive(true); //circle1.transform.position = hit1.point; //Debug.Log("circle1 initial = " + circle1.transform.position); //讓agent1移動到hit1.point agent1.SetDestination(hit1.point); //設定player_animator的參數status=1(走路) //agent1.GetComponent().SetInteger("status", 1); //如果點按物件=jump2,或jump3,就跳躍 if (hit1.collider.gameObject.name == "jump2") { agent1.GetComponent().SetInteger("status", 2); } else if (hit1.collider.gameObject.name == "jump3") { agent1.GetComponent().SetInteger("status", 2); } else { //這邊是設定,只有走一下(就是滑鼠點按下時,走一下) //若要一直走,必須在update裡面設定距離>0.2,就一直走 agent1.GetComponent().SetInteger("status", 1); } } } //計算agent1與圓圈的距離 float distance1 = Vector3.Distance(agent1.transform.position, circle1.transform.position); if (distance1 < 0.1f) { isclose = true; agent1.GetComponent().SetInteger("status", 0); } } }