Game Development unity – Navmesh brokers attempting to go to destroyed objects Team FunTrove - September 21, 2022 0 [ad_1] unity – Navmesh brokers attempting to go to destroyed objects – Game Development Stack Exchange Stack Exchange Network Stack Exchange community consists of 182 Q&A communities together with Stack Overflow, the biggest, most trusted on-line neighborhood for builders to be taught, share their information, and construct their careers. Visit Stack Exchange Log in Sign up Game Development Stack Exchange is a query and reply web site for skilled and unbiased recreation builders. It solely takes a minute to enroll. Sign as much as be part of this neighborhood Anybody can ask a query Anybody can reply The finest solutions are voted up and rise to the highest Asked right now Viewed 6 instances $begingroup$ I’m nonetheless engaged on the RTS and have added a system that permits enemies to randomly wander. However, when a number of enemies combat one in every of my items, they are going to simply clump up Afterwords as an alternative of going again to wandering. Here is the code for his or her pathfinding. utilizing System.Collections; utilizing System.Collections.Generic; utilizing UnityEngine; utilizing UnityEngine.AI; utilizing UnityEngine.UIElements; public class Human : MonoBehaviour { public List<GameObject> axolotls; Detector detector; NavMeshAgent myAgent; GameObject currentAxolotl; Vector3 humanPosition; Vector3 axolotlPosition; float distanceToAxolotl; float lowestDistance = 0; public GameObject closestAxolotl; Vector3 randomWander; // Start is named earlier than the primary body replace void Start() { detector = this.gameObject.GetComponentInChildren<Detector>(); myAgent = this.gameObject.GetComponent<NavMeshAgent>(); } // Update is named as soon as per body void Update() { axolotls = detector.axolotls; //loop over all detected axolotls for (int index = 0; index < axolotls.Count; index++) { //Debug.Log("loop began:"+index); currentAxolotl = axolotls[index]; if (currentAxolotl != null) { humanPosition = this.gameObject.rework.place; axolotlPosition = currentAxolotl.rework.place; distanceToAxolotl = Vector3.Distance(humanPosition, axolotlPosition); if (lowestDistance == 0) { lowestDistance = distanceToAxolotl; closestAxolotl = currentAxolotl; Debug.Log("axolotl set as a result of closest axolotl was equal to null"); } else if (distanceToAxolotl < lowestDistance) { lowestDistance = distanceToAxolotl; closestAxolotl = currentAxolotl; } if (axolotls.Count == 0) { lowestDistance = 0; Debug.Log("lowest distance reset as a result of record was empty"); myAgent.SetDestination(randomDestination(randomWander)); } } } if (closestAxolotl != null) { if (!axolotls.Contains(closestAxolotl)) { axolotls.Remove(closestAxolotl); closestAxolotl = null; myAgent.SetDestination(randomDestination(randomWander)); } if (!UnitSelections.Instance.unitList.Contains(closestAxolotl)) { axolotls.Remove(closestAxolotl); closestAxolotl = null; myAgent.SetDestination(randomDestination(randomWander)); } } if (closestAxolotl == null) { // determine a approach of checking if the human goes to its random vacation spot as an alternative of if it has a path. if (!myAgent.hasPath) { myAgent.SetDestination(randomDestination(randomWander)); } } if (closestAxolotl != null) { myAgent.SetDestination(axolotlPosition); } } public void removeAxolotl(GameObject axolotl) { detector.axolotls.Remove(axolotl); if(closestAxolotl == axolotl) { closestAxolotl = null; lowestDistance = 0; } } Vector3 randomDestination(Vector3 randomDestination) { randomDestination.x = Random.Range(-27, 27); randomDestination.z = Random.Range(-27, 27); Debug.Log("setting vacation spot"); return randomDestination; } } DMGregory♦ 117k2121 gold badges211211 silver badges317317 bronze badges requested 3 hours in the past $endgroup$ 1 You should log in to reply this query. lang-cs Your privateness By clicking “Accept all cookies”, you agree Stack Exchange can retailer cookies in your system and disclose info in accordance with our Cookie Policy. Accept all cookies Customize settings [ad_2] LEAVE A REPLY Cancel reply Please enter your comment! Please enter your name here You have entered an incorrect email address! Please enter your email address here Save my name, email, and website in this browser for the next time I comment.