77 lines
1.7 KiB
C#
77 lines
1.7 KiB
C#
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(vp_Shooter))]
|
|
public class vp_SimpleAITurret : MonoBehaviour
|
|
{
|
|
public float ViewRange = 10f;
|
|
|
|
public float AimSpeed = 50f;
|
|
|
|
public float WakeInterval = 2f;
|
|
|
|
public float FireAngle = 10f;
|
|
|
|
protected vp_Shooter m_Shooter;
|
|
|
|
protected Transform m_Transform;
|
|
|
|
protected Transform m_Target;
|
|
|
|
protected vp_Timer.Handle m_Timer = new vp_Timer.Handle();
|
|
|
|
protected virtual void Start()
|
|
{
|
|
m_Shooter = GetComponent<vp_Shooter>();
|
|
m_Transform = base.transform;
|
|
}
|
|
|
|
protected virtual void Update()
|
|
{
|
|
if (!m_Timer.Active)
|
|
{
|
|
vp_Timer.In(WakeInterval, delegate
|
|
{
|
|
if (m_Target == null)
|
|
{
|
|
m_Target = ScanForLocalPlayer();
|
|
}
|
|
else
|
|
{
|
|
m_Target = null;
|
|
}
|
|
}, m_Timer);
|
|
}
|
|
if (m_Target != null)
|
|
{
|
|
AttackTarget();
|
|
}
|
|
}
|
|
|
|
protected virtual Transform ScanForLocalPlayer()
|
|
{
|
|
Collider[] array = Physics.OverlapSphere(m_Transform.position, ViewRange, 1073741824);
|
|
Collider[] array2 = array;
|
|
foreach (Collider collider in array2)
|
|
{
|
|
RaycastHit hitInfo;
|
|
Physics.Linecast(m_Transform.position, collider.transform.position + Vector3.up, out hitInfo);
|
|
if (!(hitInfo.collider != null) || !(hitInfo.collider != collider))
|
|
{
|
|
return collider.transform;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
protected virtual void AttackTarget()
|
|
{
|
|
Vector3 forward = m_Target.GetComponent<Collider>().bounds.center - m_Transform.position;
|
|
Quaternion to = Quaternion.LookRotation(forward);
|
|
m_Transform.rotation = Quaternion.RotateTowards(m_Transform.rotation, to, Time.deltaTime * AimSpeed);
|
|
if (Mathf.Abs(vp_3DUtility.LookAtAngleHorizontal(m_Transform.position, m_Transform.forward, m_Target.position)) < FireAngle)
|
|
{
|
|
m_Shooter.TryFire();
|
|
}
|
|
}
|
|
}
|