[ad_1]
Game Development Stack Exchange is a query and reply web site for skilled and unbiased recreation builders. It solely takes a minute to enroll.
Anybody can ask a query
Anybody can reply
The finest solutions are voted up and rise to the highest
Asked
Viewed
6 instances
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;
}
}
$endgroup$
1
You should log in to reply this query.
lang-cs
[ad_2]