97 lines
2.7 KiB
C#
97 lines
2.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using DarkTonic.MasterAudio;
|
|
using UnityEngine;
|
|
using Mirror;
|
|
|
|
public class EnemyPlane : NetworkBehaviour
|
|
{
|
|
[Header("Movement")]
|
|
public float speed = 10f; // 飞机速度
|
|
public float lifeDistance = 200f; // 飞机飞行多少米后销毁
|
|
|
|
[Header("Drop Effect")]
|
|
public float dropStartDistance = 50f; // 开始投弹的距离
|
|
public float dropEffectDuration = 3f; // 投弹特效持续时间
|
|
public GameObject dropEffectObj; // 飞机子物体上的投弹特效
|
|
|
|
public Vector3 targetPosition;
|
|
|
|
private float traveled = 0f;
|
|
private bool hasDropped = false;
|
|
private Vector3 startPos;
|
|
private Vector3 flyDir; // 固定飞行方向
|
|
private float fixedY; // 固定的高度
|
|
|
|
public override void OnStartServer()
|
|
{
|
|
base.OnStartServer();
|
|
startPos = transform.position;
|
|
targetPosition = GameLocal.Ins.endMortarPos.position;
|
|
|
|
Vector3 diff = targetPosition - startPos;
|
|
diff.y = 0; // 不考虑高度
|
|
|
|
// 判断哪个方向更大 → 固定为 X 或 Z 方向
|
|
if (Mathf.Abs(diff.x) > Mathf.Abs(diff.z))
|
|
{
|
|
flyDir = new Vector3(Mathf.Sign(diff.x), 0, 0); // 飞 X 轴正/负方向
|
|
}
|
|
else
|
|
{
|
|
flyDir = new Vector3(0, 0, Mathf.Sign(diff.z)); // 飞 Z 轴正/负方向
|
|
}
|
|
|
|
// 固定初始高度
|
|
fixedY = transform.position.y;
|
|
|
|
// 设置朝向
|
|
if (flyDir != Vector3.zero)
|
|
transform.rotation = Quaternion.LookRotation(flyDir, Vector3.up);
|
|
|
|
if (dropEffectObj != null)
|
|
dropEffectObj.SetActive(false);
|
|
}
|
|
|
|
|
|
void Update()
|
|
{
|
|
if (!isServer) return;
|
|
|
|
float delta = Time.deltaTime;
|
|
|
|
// 固定方向移动
|
|
Vector3 deltaPos = flyDir * speed * delta;
|
|
transform.position += deltaPos;
|
|
|
|
// 保持 Y 不变
|
|
transform.position = new Vector3(transform.position.x, fixedY, transform.position.z);
|
|
|
|
traveled = Vector3.Distance(startPos, transform.position);
|
|
|
|
// 投弹
|
|
if (!hasDropped && traveled >= dropStartDistance)
|
|
{
|
|
hasDropped = true;
|
|
if (dropEffectObj != null)
|
|
StartCoroutine(DropEffectRoutine());
|
|
}
|
|
|
|
// 飞机寿命结束
|
|
if (traveled >= lifeDistance)
|
|
{
|
|
NetworkServer.Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
|
|
IEnumerator DropEffectRoutine()
|
|
{
|
|
MasterAudio.PlaySound3DAtVector3("1.54", transform.position);
|
|
dropEffectObj.SetActive(true);
|
|
yield return new WaitForSeconds(dropEffectDuration);
|
|
if (dropEffectObj != null)
|
|
dropEffectObj.SetActive(false);
|
|
}
|
|
}
|