using UnityEngine; using System.Collections; namespace Script.Gameplay.Facility { public class TianHeController : BaseFacilityController { [SerializeField] private GameObject lidPrefab; [SerializeField] private AudioClip openSound; [SerializeField] private AudioSource audioSource; private Coroutine lidCoroutine; [SerializeField] private Vector3 closedPos = new Vector3(0f, 0f, 0f); [SerializeField] private Vector3 openPos = new Vector3(0f, 0.5f, 0f); [SerializeField] private float lidMoveDuration = 1f; // 盖子动画时长 public override void OnSignalReceived(bool active, GameObject sender) { base.OnSignalReceived(active, sender); // 停止之前的动画 if (lidCoroutine != null) StopCoroutine(lidCoroutine); // 启动新的动画 lidCoroutine = StartCoroutine(MoveLid(active ? openPos : closedPos)); if (audioSource != null && openSound != null && active) { audioSource.clip = openSound; audioSource.Play(); } } private IEnumerator MoveLid(Vector3 targetPos) { Vector3 startPos = lidPrefab.transform.localPosition; float elapsed = 0f; while (elapsed < lidMoveDuration) { elapsed += Time.deltaTime; lidPrefab.transform.localPosition = Vector3.Lerp(startPos, targetPos, elapsed / lidMoveDuration); yield return null; } lidPrefab.transform.localPosition = targetPos; } } }