103 lines
2.8 KiB
C#
103 lines
2.8 KiB
C#
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// 火车
|
|
/// </summary>
|
|
public class Train : MonoBehaviour
|
|
{
|
|
/// <summary>
|
|
/// 火车对象
|
|
/// </summary>
|
|
public GameObject trainObj;
|
|
|
|
/// <summary>
|
|
/// 左点
|
|
/// </summary>
|
|
public Transform leftPoint;
|
|
|
|
/// <summary>
|
|
/// 右点
|
|
/// </summary>
|
|
public Transform rightPoint;
|
|
|
|
/// <summary>
|
|
/// 方向
|
|
/// </summary>
|
|
private E_DirectionType e_DirectionType;
|
|
|
|
/// <summary>
|
|
/// 刚体
|
|
/// </summary>
|
|
public Rigidbody rb;
|
|
|
|
/// <summary>
|
|
/// 是否死亡
|
|
/// </summary>
|
|
private bool isDie;
|
|
|
|
/// <summary>
|
|
/// 是否预警
|
|
/// </summary>
|
|
private bool isEarlyWarning;
|
|
|
|
private void OnEnable()
|
|
{
|
|
isEarlyWarning = false;
|
|
isDie = false;
|
|
rb.useGravity = false;
|
|
rb.isKinematic = true;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
rb.velocity = Vector3.zero;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (Vector3.Distance(Scenes.Instance.player.transform.position, this.transform.position) < 120&& !isDie)
|
|
{
|
|
if (!isEarlyWarning)
|
|
{
|
|
AudioManager.Instance.PlaySound(YooAssetManager.Instance.LoadAssetSync<AudioClip>(PackagesName.GameData, "TrainPass"));
|
|
UIManager.Instance.GetUI<GameUI>().SetEarlyWarning(e_DirectionType);
|
|
isEarlyWarning = true;
|
|
}
|
|
trainObj.transform.Translate(trainObj.transform.forward * Scenes.Instance.gameScene.trainMoveSpeed * Time.deltaTime, Space.World);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 击飞
|
|
/// </summary>
|
|
public void ShootFly(Transform transform)
|
|
{
|
|
if (isDie) return;
|
|
isDie = true;
|
|
rb.useGravity = true;
|
|
rb.isKinematic = false;
|
|
rb.AddForce((transform.position - this.transform.position).normalized * -100, ForceMode.Impulse);
|
|
rb.AddForce(Vector3.up * 20, ForceMode.Impulse);
|
|
rb.AddTorque(new Vector3(Random.Range(-180, 180), Random.Range(-180, 180), Random.Range(-180, 180)), ForceMode.Impulse);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置方向
|
|
/// </summary>
|
|
public void SetDirection(E_DirectionType e_DirectionType)
|
|
{
|
|
this.e_DirectionType = e_DirectionType;
|
|
switch (e_DirectionType)
|
|
{
|
|
case E_DirectionType.Left:
|
|
trainObj.transform.position = leftPoint.position;
|
|
trainObj.transform.localEulerAngles = new Vector3(0, 180, 0);
|
|
break;
|
|
case E_DirectionType.Right:
|
|
trainObj.transform.position = rightPoint.position;
|
|
trainObj.transform.localEulerAngles = new Vector3(0, 0, 0);
|
|
break;
|
|
}
|
|
}
|
|
}
|