207 lines
6.0 KiB
C#
207 lines
6.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using DG.Tweening;
|
|
using System.Linq;
|
|
|
|
/// <summary>
|
|
/// 无人机
|
|
/// </summary>
|
|
public class UnmannedAerialVehicle : MonoBehaviour
|
|
{
|
|
/// <summary>
|
|
/// 所属无人机蜂群
|
|
/// </summary>
|
|
public UnmannedAerialVehicleManage unmannedAerialVehicleManage;
|
|
|
|
#region 无人机数据
|
|
/// <summary>
|
|
/// 续航时间
|
|
/// </summary>
|
|
public string batteryLife;
|
|
/// <summary>
|
|
/// 抗风等级
|
|
/// </summary>
|
|
public string classificationWindResistance;
|
|
/// <summary>
|
|
/// 最大飞行速度
|
|
/// </summary>
|
|
public string maximumFlyingSpeed;
|
|
/// <summary>
|
|
/// RCS
|
|
/// </summary>
|
|
public string RCS;
|
|
/// <summary>
|
|
/// 卫星定位频点
|
|
/// </summary>
|
|
public string satellitePositioningFrequency;
|
|
/// <summary>
|
|
/// 数据链通信频点
|
|
/// </summary>
|
|
public string dataLinkCommunicationFrequency;
|
|
/// <summary>
|
|
/// 电子侦察能力
|
|
/// </summary>
|
|
public string electronicReconnaissanceCapability;
|
|
/// <summary>
|
|
/// 光学侦察能力
|
|
/// </summary>
|
|
public string opticalReconnaissanceCapability;
|
|
#endregion
|
|
/// <summary>
|
|
/// 飞行速度
|
|
/// </summary>
|
|
public float FireSpeed = 20.0f;
|
|
/// <summary>
|
|
/// 检测范围半径
|
|
/// </summary>
|
|
public float detectionRadius = 50; //
|
|
/// <summary>
|
|
/// 是否正在攻击目标
|
|
/// </summary>
|
|
private bool isEngagedTarget = false;
|
|
/// <summary>
|
|
/// 爆炸预制体
|
|
/// </summary>
|
|
public GameObject explodePrefab;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (unmannedAerialVehicleManage && unmannedAerialVehicleManage.isStartRehearsing)
|
|
{
|
|
switch (unmannedAerialVehicleManage.pattern)
|
|
{
|
|
case Pattern.待机:
|
|
break;
|
|
case Pattern.警戒:
|
|
AttackATarget();
|
|
break;
|
|
case Pattern.攻击:
|
|
AttackATarget();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 攻击目标
|
|
/// </summary>
|
|
public void AttackATarget()
|
|
{
|
|
if (!isEngagedTarget)
|
|
{
|
|
List<Collider> colliders = Physics.OverlapSphere(transform.position, detectionRadius).ToList(); // 检索范围内的所有碰撞体
|
|
List<Collider> colliders1 = colliders.FindAll(x => x.transform.tag == "AttackTarget");
|
|
if (colliders1.Count>0)
|
|
{
|
|
int _number= Random.Range(0, colliders1.Count-1);
|
|
isEngagedTarget = true;
|
|
Debug.Log(colliders1[_number].transform.name);
|
|
AttAck(colliders1[_number].transform);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发起攻击
|
|
/// </summary>
|
|
private void AttAck(Transform target)
|
|
{
|
|
Debug.Log("开始攻击目标..:" + target.name);
|
|
Vector3 _v3 = target.position;
|
|
transform.DOLookAt(_v3, 0.1f).OnComplete(() => {
|
|
float distance = Vector3.Distance(transform.position, target.position);
|
|
transform.DOMove(target.position, distance / FireSpeed).SetEase(Ease.Linear).OnComplete(()=> {
|
|
if (!target)
|
|
{
|
|
BeAssaulted("没有攻击到目标");
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
if (other.tag == "AttackTarget")
|
|
{
|
|
// 销毁objectToDestroy对象
|
|
BeAssaulted("攻击到目标");
|
|
GameObject Bao = Instantiate(explodePrefab, other.transform);
|
|
Bao.transform.localPosition = Vector3.zero;
|
|
Bao.transform.SetParent(null);
|
|
Bao.SetActive(true);
|
|
Destroy(other.gameObject);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 被攻击
|
|
/// </summary>
|
|
/// <param name="type"></param>
|
|
public void BeAssaulted(string type)
|
|
{
|
|
switch (type) {
|
|
case "激光打击":
|
|
GameObject Bao1 = Instantiate(explodePrefab, transform);
|
|
Bao1.transform.localPosition = Vector3.zero;
|
|
Bao1.transform.SetParent(null);
|
|
Bao1.SetActive(true);
|
|
DistroyThis();
|
|
Debug.Log(transform.name+"被激光打击销毁了");
|
|
break;
|
|
case "无线电干扰":
|
|
Vector3 _pos = transform.position - new Vector3(0, 30, 0);
|
|
transform.LookAt(_pos);
|
|
transform.DOMove(_pos, 1).OnComplete(() => {
|
|
GameObject Bao2 = Instantiate(explodePrefab, transform);
|
|
Bao2.transform.localPosition = Vector3.zero;
|
|
Bao2.transform.SetParent(null);
|
|
Bao2.SetActive(true);
|
|
DistroyThis();
|
|
});
|
|
Debug.Log(transform.name + "无人机被无线电干扰销毁了");
|
|
break;
|
|
case "攻击到目标":
|
|
GameObject Bao3 = Instantiate(explodePrefab, transform);
|
|
Bao3.transform.localPosition = Vector3.zero;
|
|
Bao3.transform.SetParent(null);
|
|
Bao3.SetActive(true);
|
|
DistroyThis();
|
|
Debug.Log(transform.name + "无人机自杀式攻击销毁了");
|
|
break;
|
|
case "没有攻击到目标":
|
|
GameObject Bao4 = Instantiate(explodePrefab, transform);
|
|
Bao4.transform.localPosition = Vector3.zero;
|
|
Bao4.transform.SetParent(null);
|
|
Bao4.SetActive(true);
|
|
DistroyThis();
|
|
Debug.Log(transform.name + "无人机自杀式销毁了");
|
|
break;
|
|
default:
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void DistroyThis()
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
|
|
void OnDestroy()
|
|
{
|
|
transform.DOKill();
|
|
//
|
|
}
|
|
}
|