...
This commit is contained in:
62
Assets/Scripts/Weapons/AimAssistant.cs
Normal file
62
Assets/Scripts/Weapons/AimAssistant.cs
Normal file
@ -0,0 +1,62 @@
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class AutoAim : MonoBehaviour
|
||||
{
|
||||
// Start is called before the first frame update
|
||||
public GameObject enemy;
|
||||
|
||||
public float lookSpeed = 200f;
|
||||
|
||||
public GameObject player;
|
||||
public Camera camera;
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
Vector3 direction = enemy.transform.position - camera.transform.position;
|
||||
Quaternion targetRotation = Quaternion.LookRotation(direction);
|
||||
Quaternion lookAt = Quaternion.RotateTowards(camera.transform.rotation, targetRotation, Time.deltaTime * lookSpeed);
|
||||
camera.transform.rotation = lookAt;
|
||||
|
||||
}
|
||||
}
|
@ -24,13 +24,6 @@ public class Shooting : MonoBehaviour
|
||||
public GameObject gun;
|
||||
void Update()
|
||||
{
|
||||
/*if (Input.GetButtonDown("Fire1"))
|
||||
{
|
||||
Prefab = Random.Range(0, 1);
|
||||
Instantiate(Prefabs[Prefab], FirePoint.transform.position, FirePoint.transform.rotation);
|
||||
Shoot();
|
||||
}*/
|
||||
//if(Input.GetButtonDown("")
|
||||
if (Input.GetMouseButton(0) && fireCountdown <= 0f)
|
||||
{
|
||||
Instantiate(Prefabs[Prefab], FirePoint.transform.position, FirePoint.transform.rotation);
|
||||
@ -41,27 +34,14 @@ public class Shooting : MonoBehaviour
|
||||
fireCountdown -= Time.deltaTime;
|
||||
}
|
||||
|
||||
void Shoot()
|
||||
public void Shoot()
|
||||
{
|
||||
RaycastHit hit;
|
||||
if (Physics.Raycast(raycast.transform.position, raycast.transform.forward, out hit, range))
|
||||
if (Physics.Raycast(raycast.transform.position, raycast.transform.forward, out var hit, range))
|
||||
{
|
||||
Debug.Log(hit.transform.name);
|
||||
|
||||
Target target = hit.transform.GetComponent<Target>();
|
||||
|
||||
if (target != null)
|
||||
{
|
||||
if (hit.transform.TryGetComponent<Target>(out var target))
|
||||
target.TakeDamage(damage);
|
||||
}
|
||||
RotateToGunDirection(gun, hit.point);
|
||||
}
|
||||
}
|
||||
|
||||
void RotateToGunDirection(GameObject obj, Vector3 destination)
|
||||
{
|
||||
direction = destination - obj.transform.position;
|
||||
rotation = Quaternion.LookRotation(direction);
|
||||
obj.transform.localRotation = Quaternion.Lerp(obj.transform.rotation, rotation, 1);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user