rangeEnemyFinder.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. //only consider enemies that are in chase range and have a target
  24. if(!enemy.isInChaseRange || enemy.target == null) continue;
  25. float dist = Vector3.Distance((Vector2)enemy.transform.position,(Vector2)transform.position);
  26. if(dist < radius){
  27. if(dist < closestDist){
  28. targetEnemy = enemy;
  29. closestDist = dist;
  30. }
  31. }
  32. }
  33. lockIndicator.gameObject.SetActive(targetEnemy!= null);
  34. if(targetEnemy != null){
  35. //show indicator
  36. lockIndicator.position = targetEnemy.transform.position + new Vector3 (0, 0.62f, 0);
  37. }
  38. }
  39. }