Almost Final Version
This commit is contained in:
@ -1,62 +1,97 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(NPC))]
|
||||
public class AimAssistant : MonoBehaviour
|
||||
{
|
||||
public GameObject enemy;
|
||||
public bool _isFiring = false;
|
||||
[HideInInspector] public GameObject enemy;
|
||||
private Shooting _shooting;
|
||||
public bool isFiring = false;
|
||||
private ICharacter _myNpc;
|
||||
public float lookSpeed = 200f;
|
||||
[SerializeField] private float lookSpeed = 400f;
|
||||
private Transform _myTransform;
|
||||
private float _fireCountdown;
|
||||
|
||||
//Я пытался придумать как обойти костыли с корутиной, но как видите ничего кроме других костылей не придумал.
|
||||
private bool _firelock = false;
|
||||
public bool fireAnimation = false;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_myNpc = GetComponent<NPC>();
|
||||
_shooting = GetComponent<Shooting>();
|
||||
_myTransform = transform;
|
||||
_fireCountdown = 1f / SettingsReader.Instance.GetSettings.RateOfFire;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
private void FixedUpdate()
|
||||
{
|
||||
//Ищем противника на сцене.
|
||||
if (_isFiring == false)
|
||||
if (isFiring == false)
|
||||
{
|
||||
var enemies = GameManager.GetVisibleEnemies(_myNpc.GetCharacter.Team, transform.position);
|
||||
enemy = enemies[new System.Random().Next(enemies.Count)];
|
||||
var enemies = GameManager.GetVisibleEnemies(_myNpc.GetCharacter.Team.GetOppositeTeam(), transform.position);
|
||||
if (enemies.Count == 0)
|
||||
{
|
||||
StopShooting();
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
enemy = enemies[new System.Random().Next(enemies.Count)];
|
||||
var character = enemy.GetComponent<ICharacter>();
|
||||
character.OnDeathEvent += _ => _isFiring = false;
|
||||
_isFiring = true;
|
||||
character.OnDeathEvent += _ => isFiring = false;
|
||||
isFiring = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//Raycast до противника enemy
|
||||
var range = 200f;
|
||||
while (true)
|
||||
if (Physics.Raycast(_myTransform.position, _myTransform.forward,
|
||||
out var hit, SettingsReader.Instance.GetSettings.ViewDistance))
|
||||
{
|
||||
if (!Physics.Raycast(this.transform.position, this.transform.forward, out var hit, range))
|
||||
if (hit.collider.gameObject != enemy)
|
||||
{
|
||||
_isFiring = false;
|
||||
StopCoroutine(nameof(Shooting));
|
||||
StopShooting();
|
||||
enemy = null;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
GetComponent<Shooting>().Shoot();
|
||||
}
|
||||
}
|
||||
|
||||
if (_firelock == false)
|
||||
{
|
||||
fireAnimation = true;
|
||||
StartCoroutine(nameof(Shooting));
|
||||
}
|
||||
_firelock = true;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
if (enemy != null)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void StopShooting()
|
||||
{
|
||||
print("stop shooting");
|
||||
_firelock = false;
|
||||
isFiring = false;
|
||||
fireAnimation = false;
|
||||
}
|
||||
|
||||
private IEnumerator Shooting()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
print("in Shooting");
|
||||
_shooting.Shoot();
|
||||
yield return new WaitForSeconds(_fireCountdown);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user