12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class rangeEnemyFinder : MonoBehaviour
- {
- public float radius = 10;
- public enemyScript[] enemies;
- public Transform lockIndicator;
- private void OnDrawGizmos() {
- Gizmos.DrawWireSphere(transform.position, radius);
- }
- void Start()
- {
-
- }
- // Update is called once per frame
- public enemyScript targetEnemy;
-
- void Update()
- {
- enemies = FindObjectsOfType<enemyScript>();
- float closestDist = radius * 10f;
- targetEnemy = null;
- foreach(enemyScript enemy in enemies){
- float dist = Vector3.Distance((Vector2)enemy.transform.position,(Vector2)transform.position);
- if(dist < radius){
- if(dist < closestDist){
- targetEnemy = enemy;
- closestDist = dist;
- }
- }
- }
- lockIndicator.gameObject.SetActive(targetEnemy!= null);
- if(targetEnemy != null){
- //show indicator
- lockIndicator.position = targetEnemy.transform.position + new Vector3 (0, 0.62f, 0);
- }
- }
- }
|