127 lines
4.1 KiB
C#
127 lines
4.1 KiB
C#
using DG.Tweening;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using static UnityEngine.GraphicsBuffer;
|
|
|
|
public class WingmanController : MonoBehaviour
|
|
{
|
|
public Transform player; // 主机(玩家飞机)
|
|
public float followDistance = 30f; // 僚机与主机的跟随距离
|
|
public float followHeight = 20f; // 僚机与主机之间的高度差
|
|
public float followLR = 10;
|
|
public float followSpeed = 5f; // 僚机跟随的速度
|
|
public float rotationSpeed = 3f; // 僚机旋转速度
|
|
public float speed = 10f; // 导弹的速度
|
|
public Transform target; // 导弹的目标
|
|
public GameObject missilePrefab; // 僚机的导弹
|
|
public Transform missileSpawnPoint; // 僚机发射导弹的位置
|
|
public float distance;
|
|
public float attackInterval = 0.3f;
|
|
private float lastAttackTime = 0f;
|
|
private void Start()
|
|
{
|
|
player = GameObject.FindWithTag("Player").transform;
|
|
missileSpawnPoint = this.transform;
|
|
|
|
|
|
}
|
|
public GameObject FindClosestEnemy()
|
|
{
|
|
// 获取所有敌人
|
|
GameObject[] enemies = GameObject.FindGameObjectsWithTag("enemy");
|
|
|
|
// 如果没有找到敌人,返回空
|
|
if (enemies.Length == 0)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
GameObject closestEnemy = null;
|
|
float closestDistance = Mathf.Infinity; // 设置初始距离为无限大
|
|
|
|
// 遍历所有敌人,找到最近的敌人
|
|
foreach (GameObject enemy in enemies)
|
|
{
|
|
// 计算敌人和当前物体之间的距离
|
|
float distance = Vector3.Distance(transform.position, enemy.transform.position);
|
|
|
|
// 如果该敌人比当前最近的敌人距离更近
|
|
if (distance < closestDistance)
|
|
{
|
|
closestEnemy = enemy; // 更新最近敌人
|
|
closestDistance = distance; // 更新最小距离
|
|
}
|
|
}
|
|
return closestEnemy;
|
|
}
|
|
private void Update()
|
|
{
|
|
// 让僚机跟随主机
|
|
FollowPlayer();
|
|
|
|
|
|
AutoAttack();
|
|
|
|
// 让僚机开火
|
|
|
|
|
|
|
|
}
|
|
void AutoAttack()
|
|
{
|
|
// 获取爆炸范围内的所有目标
|
|
Collider[] colliders = Physics.OverlapBox(transform.position, new Vector3(200, 200, 200));
|
|
|
|
foreach (Collider collider in colliders)
|
|
{
|
|
// 计算与目标的距离
|
|
float dis = Vector3.Distance(transform.position, collider.transform.position);
|
|
|
|
if (dis > distance) return;
|
|
|
|
// 检查是否满足攻击间隔时间要求
|
|
if (Time.time - lastAttackTime < attackInterval) return;
|
|
|
|
//具体伤害
|
|
if (collider.gameObject.tag == "enemy")
|
|
{
|
|
FireMissile();
|
|
lastAttackTime = Time.time;
|
|
}
|
|
|
|
}
|
|
}
|
|
// 跟随主机
|
|
private void FollowPlayer()
|
|
{
|
|
Vector3 desiredPosition = player.position - player.forward * followDistance + Vector3.up * followHeight+ Vector3.back* followLR;
|
|
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, followSpeed * Time.deltaTime);
|
|
transform.position = smoothedPosition;
|
|
|
|
// 僚机朝向主机的方向
|
|
Quaternion targetRotation = Quaternion.LookRotation(player.position - transform.position);
|
|
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
|
|
}
|
|
|
|
// 僚机发射导弹
|
|
private void FireMissile()
|
|
{
|
|
target = FindClosestEnemy().transform;
|
|
|
|
//Todo僚机追踪子弹
|
|
if (target != null)
|
|
{
|
|
GameObject missile = Instantiate(missilePrefab, missileSpawnPoint.position, missileSpawnPoint.rotation);
|
|
Vector3 direction = target.position - missile.transform.position;
|
|
Quaternion targetRotation = Quaternion.LookRotation(direction);
|
|
missile.transform.rotation = targetRotation;
|
|
float distance = Vector3.Distance(missile.transform.position, target.position);
|
|
float moveDuration = Mathf.Clamp(distance / 20f, 0.2f, 0.7f);
|
|
missile.transform.DOMove(target.position, moveDuration);
|
|
}
|
|
}
|
|
}
|