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

I have an array of players. How can I get my enemy to go to the closest one?

$
0
0
I've tried this: public GameObject[] enemies; public Transform curTarget; public float[] enemyDists; public float curEnemyDist; public int count = 1; // Use this for initialization void Start () { } // Update is called once per frame void Update () { enemies = GameObject.FindGameObjectsWithTag ("Player"); foreach (GameObject enemy in enemies) { enemyDists[count - 1] = Vector3.Distance (transform.position, enemy.transform.position); count +=1; } count = 1; foreach (float dist in enemyDists) { if(dist < curEnemyDist) { Debug.Log("HELLO"); } } //This is constantly update the targetw ith the new/current target in this script.. GetComponent ().target = curTarget; curEnemyDist = Vector3.Distance (transform.position, curTarget.transform.position); foreach (float dist in enemyDists) { } } But it ain't working. I need to grab the transform of the gameobject in the array and get the distance between that and this.transform. How can I do that?

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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