You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
944 B
45 lines
944 B
using Ai; |
|
using UnityEngine; |
|
|
|
namespace Player |
|
{ |
|
public class TargetClosestEnemy : MonoBehaviour |
|
{ |
|
|
|
[SerializeField] private GameObject _lock; |
|
private TargetingEnemy _enemy; |
|
private GameObject _oldLock; |
|
|
|
public Vector3 ClosestPosition() |
|
{ |
|
var closest = Vector3.forward; |
|
var distance = float.MaxValue; |
|
TargetingEnemy localEnemy = null; |
|
foreach (var enemy in AiController.Instance.TargetingEnemies) |
|
{ |
|
if (!enemy.IsTargetable) |
|
continue; |
|
var current = Vector3.Distance(enemy.transform.position, transform.position); |
|
if (distance > current) |
|
{ |
|
distance = current; |
|
closest = enemy.transform.position; |
|
localEnemy = enemy; |
|
} |
|
} |
|
|
|
if (localEnemy != null && localEnemy != _enemy) |
|
{ |
|
if (_oldLock != null) |
|
Destroy(_oldLock); |
|
|
|
_enemy = localEnemy; |
|
_oldLock = Instantiate(_lock, _enemy.transform); |
|
_enemy.Lock = _oldLock; |
|
} |
|
|
|
return closest; |
|
} |
|
|
|
} |
|
}
|
|
|