rangeEnemyFinder.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class rangeEnemyFinder : MonoBehaviour
  5. {
  6. public float radius = 10;
  7. public enemyScript[] enemies;
  8. public Transform lockIndicator;
  9. private void OnDrawGizmos() {
  10. Gizmos.DrawWireSphere(transform.position, radius);
  11. }
  12. void Start()
  13. {
  14. }
  15. // Update is called once per frame
  16. public enemyScript targetEnemy;
  17. void Update()
  18. {
  19. enemies = FindObjectsOfType<enemyScript>();
  20. float closestDist = radius * 10f;
  21. targetEnemy = null;
  22. foreach(enemyScript enemy in enemies){
  23. float dist = Vector3.Distance((Vector2)enemy.transform.position,(Vector2)transform.position);
  24. if(dist < radius){
  25. if(dist < closestDist){
  26. targetEnemy = enemy;
  27. closestDist = dist;
  28. }
  29. }
  30. }
  31. lockIndicator.gameObject.SetActive(targetEnemy!= null);
  32. if(targetEnemy != null){
  33. //show indicator
  34. lockIndicator.position = targetEnemy.transform.position + new Vector3 (0, 0.62f, 0);
  35. }
  36. }
  37. }