63 lines
1.8 KiB
C#
63 lines
1.8 KiB
C#
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(NPC))]
|
|
public class AimAssistant : MonoBehaviour
|
|
{
|
|
public GameObject enemy;
|
|
public bool _isFiring = false;
|
|
private ICharacter _myNpc;
|
|
public float lookSpeed = 200f;
|
|
|
|
private void Awake()
|
|
{
|
|
_myNpc = GetComponent<NPC>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
//Ищем противника на сцене.
|
|
if (_isFiring == false)
|
|
{
|
|
var enemies = GameManager.GetVisibleEnemies(_myNpc.GetCharacter.Team, transform.position);
|
|
enemy = enemies[new System.Random().Next(enemies.Count)];
|
|
if (enemies.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
var character = enemy.GetComponent<ICharacter>();
|
|
character.OnDeathEvent += _ => _isFiring = false;
|
|
_isFiring = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//Raycast до противника enemy
|
|
var range = 200f;
|
|
while (true)
|
|
{
|
|
if (!Physics.Raycast(this.transform.position, this.transform.forward, out var hit, range))
|
|
{
|
|
_isFiring = false;
|
|
enemy = null;
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
GetComponent<Shooting>().Shoot();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
var direction = enemy.transform.position - gameObject.transform.position;
|
|
var targetRotation = Quaternion.LookRotation(direction);
|
|
var lookAt = Quaternion.RotateTowards(gameObject.transform.rotation, targetRotation,
|
|
Time.deltaTime * lookSpeed);
|
|
lookAt.z = 0;
|
|
lookAt.x = 0;
|
|
gameObject.transform.rotation = lookAt;
|
|
}
|
|
}
|