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);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,47 +1,46 @@
|
||||
using System.Collections;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Shooting : MonoBehaviour
|
||||
{
|
||||
public float damage = 10f;
|
||||
public float range = 100f;
|
||||
|
||||
public GameObject raycast;
|
||||
|
||||
public GameObject FirePoint;
|
||||
public GameObject[] Prefabs;
|
||||
public GameObject firePoint;
|
||||
[SerializeField] private GameObject projectilePrefab;
|
||||
|
||||
private float hSliderValue = 0.1f;
|
||||
private float fireCountdown = 0f;
|
||||
|
||||
private int Prefab = 1;
|
||||
|
||||
private Ray RayMouse;
|
||||
private Vector3 direction;
|
||||
private Quaternion rotation;
|
||||
private float _fireCountdown = 1f;
|
||||
|
||||
public GameObject gun;
|
||||
void Update()
|
||||
private void Update()
|
||||
{
|
||||
if (Input.GetMouseButton(0) && fireCountdown <= 0f)
|
||||
{
|
||||
Instantiate(Prefabs[Prefab], FirePoint.transform.position, FirePoint.transform.rotation);
|
||||
fireCountdown = 0;
|
||||
fireCountdown += hSliderValue;
|
||||
Shoot();
|
||||
}
|
||||
fireCountdown -= Time.deltaTime;
|
||||
HandleMouseButton();
|
||||
}
|
||||
|
||||
private void HandleMouseButton()
|
||||
{
|
||||
if (Input.GetMouseButton(0) && _fireCountdown <= 0f)
|
||||
{
|
||||
_fireCountdown = 0;
|
||||
_fireCountdown += hSliderValue;
|
||||
Shoot();
|
||||
}
|
||||
_fireCountdown -= Time.deltaTime;
|
||||
}
|
||||
|
||||
public void Shoot()
|
||||
{
|
||||
if (Physics.Raycast(raycast.transform.position, raycast.transform.forward, out var hit, range))
|
||||
if (Physics.Raycast(raycast.transform.position,
|
||||
raycast.transform.forward, out var hit,
|
||||
SettingsReader.Instance.GetSettings.ViewDistance))
|
||||
{
|
||||
Debug.Log(hit.transform.name);
|
||||
|
||||
if (hit.transform.TryGetComponent<Target>(out var target))
|
||||
target.TakeDamage(damage);
|
||||
if (hit.transform.TryGetComponent<NPC>(out var target))
|
||||
{
|
||||
Instantiate(projectilePrefab, firePoint.transform.position, firePoint.transform.rotation);
|
||||
target.GetDamage(SettingsReader.Instance.GetSettings.RifleDamage);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user