Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

C#2d Attacking multiple enemies rather than one

$
0
0
Hello! I have a list of enemies currently on the map. And my player will target the one that's closest to him. Problem is, I want to make the player target more than one of the enemies if they are in attack range of the player. Right now I'm just targetting one at the time, so I guess that's where the fault lies. How do I go about making my targets potentially multiple? Relevant code for enemy list: private GameObject target; //List-array to find enemies GameObject FindClosestEnemy() { GameObject[] gos; gos = GameObject.FindGameObjectsWithTag("Enemy"); GameObject closest = null; maxDistance = 50f; Vector3 position = transform.position; foreach (GameObject enemy in gos) { Vector3 diff = enemy.transform.position - position; float curDistance = diff.sqrMagnitude; if (curDistance < maxDistance) { closest = enemy; maxDistance = curDistance; } } return closest; } Then I declare before the player attack: GameObject target = FindClosestEnemy(); I hope you can bare with me, and I appreciate any help. I am still quite inept, haha!

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>