- Unity Artificial Intelligence Programming
- Dr. Davide Aversa Aung Sithu Kyaw Clifford Peters
- 267字
- 2021-06-10 18:57:50
The Patrol state
While our tank is in the Patrol state, we check whether it has reached the destination point. If so, it finds the next destination point to follow. The FindNextPoint method basically chooses the next random destination point among the waypoints defined. If it's still on the way to the current destination point, it'll check the distance to the player's tank. If the player's tank is in range (which we choose to be 300 here), then it switches to the Chase state. The rest of the code just rotates the tank and moves forward:
protected void UpdatePatrolState() { //Find another random patrol point if the current //point is reached if (Vector3.Distance(transform.position, destPos) <= 100.0f) { print("Reached to the destination point\n"+ "calculating the next point"); FindNextPoint(); } //Check the distance with player tank //When the distance is near, transition to chase state else if (Vector3.Distance(transform.position, playerTransform.position) <= 300.0f) { print("Switch to Chase Position"); curState = FSMState.Chase; } //Rotate to the target point Quaternion targetRotation = Quaternion.LookRotation(destPos - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * curRotSpeed); //Go Forward transform.Translate(Vector3.forward * Time.deltaTime * curSpeed); } protected void FindNextPoint() { print("Finding next point"); int rndIndex = Random.Range(0, pointList.Length); float rndRadius = 10.0f; Vector3 rndPosition = Vector3.zero; destPos = pointList[rndIndex].transform.position + rndPosition; //Check Range to decide the random point //as the same as before if (IsInCurrentRange(destPos)) { rndPosition = new Vector3(Random.Range(-rndRadius, rndRadius), 0.0f, Random.Range(-rndRadius, rndRadius)); destPos = pointList[rndIndex].transform.position + rndPosition; } } protected bool IsInCurrentRange(Vector3 pos) { float xPos = Mathf.Abs(pos.x - transform.position.x); float zPos = Mathf.Abs(pos.z - transform.position.z); if (xPos <= 50 && zPos <= 50) return true; return false; }
推薦閱讀
- EJB 3.1從入門到精通
- 物聯網智慧安監技術
- 物聯網(IoT)基礎:網絡技術+協議+用例
- 智能網聯汽車V2X與智能網聯設施I2X
- Truffle Quick Start Guide
- 計算機網絡安全實訓教程(第二版)
- 互聯網基礎資源技術與應用發展態勢(2021—2023)
- SSL VPN : Understanding, evaluating and planning secure, web/based remote access
- 網絡基礎與網絡管理項目化教程
- SAE原理與網絡規劃
- Getting Started with nopCommerce
- Enterprise ApplicationDevelopment with Ext JSand Spring
- 物聯網,So Easy!
- 巧學活用CISCO網絡典型配置
- Microsoft System Center 2012 Configuration Manager:Administration Cookbook