using UnityEngine;
public class DoorController : MonoBehaviour
{
public float openAngle = 90.0f; // 门打开的角度
public float openSpeed = 2.0f; // 门打开速度
public bool isOpen = false; // 门的状态,初始为关闭
private Quaternion closedRotation;
private Quaternion openRotation;
private void Start()
{
// 记录门的初始旋转和完全打开时的旋转
closedRotation = transform.rotation;
openRotation = Quaternion.Euler(0, openAngle, 0);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space)) // 通过按下空格键来开关门
{
ToggleDoor();
}
// 使用差值插值来平滑旋转门
if (isOpen)
{
transform.rotation = Quaternion.Slerp(transform.rotation, openRotation, Time.deltaTime * openSpeed);
}
else
{
transform.rotation = Quaternion.Slerp(transform.rotation, closedRotation, Time.deltaTime * openSpeed);
}
}
public void ToggleDoor()
{
isOpen = !isOpen;
}
}