Hey guys,
I have an enemy unit which detects a player unit within 10 distance and then goes to its position. The problem is, when there are other units within 10 units distance, the enemy unit switches back and forth between the units. Is there a way I can make my enemy unit focus on the position of a single controlled player unit instead of all of them? I want to eventuall make it so the enemy chooses the nearest unit. Thanks,
Code is posted here:
IEnumerator distanceBoolToggle(){ //included in the coroutine to save memory
while (numeratorBool==false) {
for (int i=0; i<UnitsList.Count; i++) { //this iterates through my list of player-unit objects
float distance = Vector3.Distance (transform.position, UnitsList *.transform.position);*
-
//this calculates the distance from one player-unit to enemy unit* -
if (distance <= 10) { //if the distance is less than 10 between player unit and enemy unit*
_ GameObject playerObject = UnitsList ; //creates an object for that specific player unit_
* AstarAI playerObjectDetection = playerObject.GetComponent (); //gets astarai code for that unit*
* targetPosition = playerObjectDetection.transform.position; //sets the enemy position to the pos of the player unit*
* float distanceToTargetPos = Vector3.Distance (transform.position, targetPosition); //finds distance between the two units*
* if (distanceToTargetPos > 3) {*
* seeker.StartPath (transform.position, targetPosition, OnPathComplete); //if distance is more than 3 than travel towards its destination*
* }*
* }else{*
* }*
* }*
* yield return new WaitForSeconds (1f);*
* }*
* } *
For readability put comments on the line before the code they refer to.
– Kiwasi