H_SafeExperienceDrivingSystem/U3D_DrivingSystem/Assets/Script/GateController.cs

63 lines
2.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class GateController : MonoBehaviour
{
public GameObject ganzi; // 指向“ganzi”对象的引用
private Tween currentAnimation; // 当前播放的动画Tween
private Coroutine closeTimer; // 关门计时器协程
private float closeDelay = 4f; // 关门延迟时间(秒)
public void OnTriggerEnter(GameObject other)
{
if (other.CompareTag("OpenTrigger"))
{
if (closeTimer != null)
{
StopCoroutine(closeTimer); // 停止当前的关门计时
}
if (currentAnimation != null && currentAnimation.IsPlaying())
{
currentAnimation.Kill(); // 停止当前的动画
}
currentAnimation = ganzi.transform.DOLocalRotate(new Vector3(0, ganzi.transform.localEulerAngles.y, -90f), 1f); // 使用DoTween旋转开门
closeTimer = StartCoroutine(CloseDoorTimer()); // 重置并开始关门计时
}
}
// IEnumerator RotateGanzi(float targetAngle)
// {
// // 确定当前角度和目标角度
// float currentAngle = ganzi.transform.localEulerAngles.z;
// float angleToRotate = Mathf.DeltaAngle(currentAngle, targetAngle);
//
// float duration = Mathf.Abs(angleToRotate) / 90f; // 假设以90度/秒的速度旋转
// float timeElapsed = 0;
//
// while (timeElapsed < duration)
// {
// timeElapsed += Time.deltaTime;
// float newAngle = Mathf.Lerp(currentAngle, targetAngle, timeElapsed / duration);
// ganzi.transform.localEulerAngles = new Vector3(ganzi.transform.localEulerAngles.x, ganzi.transform.localEulerAngles.y, newAngle);
// yield return null;
// }
//
// ganzi.transform.localEulerAngles = new Vector3(ganzi.transform.localEulerAngles.x, ganzi.transform.localEulerAngles.y, targetAngle);
// }
IEnumerator CloseDoorTimer()
{
yield return new WaitForSeconds(closeDelay); // 等待指定时间
if (currentAnimation != null && currentAnimation.IsPlaying())
{
currentAnimation.Kill(); // 停止当前的动画
}
currentAnimation = ganzi.transform.DOLocalRotate(new Vector3(0, ganzi.transform.localEulerAngles.y, 0f), 1f); // 使用DoTween旋转关门
}
}