LMQT/Assets/Scripts/RotateObject.cs
2024-12-10 09:03:45 +08:00

106 lines
2.5 KiB
C#

using DG.Tweening;
using UnityEngine;
public class RotateObject : MonoBehaviour
{
public float rotationSpeed = 5f; //旋转速度,度/秒
public float autoRSpeed = 10f;
public bool autoR = false;
public GameObject cam;
private Tween rotationTween; // 保存旋转的 Tween
private bool isRotating = false; // 标记是否正在旋转
private bool isPaused = false; // 标记是否暂停
public GameObject ctrl;
void Start()
{
}
//public void aROff()
//{
// autoR = false;
// transform.DORotate (Vector3.zero, 1,RotateMode.Fast);
//}
//public void aROnoff()
//{
// if(autoR)
// {
// autoR = false;
// }
// else
// {
// autoR = true;
// }
//}
void autoRotae()
{
transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
}
void Update()
{
if (isRotating && !isPaused)
{
autoRotae();
}
}
public void camTo(float dis)
{
cam.GetComponent<mouseMove>().enabled = true;
cam.GetComponent<mouseMove>().distance = dis;
cam.GetComponent<mouseMove>().x = 0;
cam.GetComponent<mouseMove>().y = 0;
}
public void camBack(Transform target)
{
cam.transform.DOMove(target.position, 1);
cam.transform.DORotate(new Vector3(.5f,0,0), 1);
cam.GetComponent<mouseMove>().distance = 4f;
cam.GetComponent<mouseMove>().x = 0;
cam.GetComponent<mouseMove>().y = 0;
cam.GetComponent<mouseMove>().enabled = false;
autoR = false;
transform.DOPlay();
}
public void ToggleRotation()
{
if (isRotating)
{
// 如果正在旋转,则暂停
isPaused = !isPaused;
ctrl.GetComponent<ObjectRotate>().enabled =true;
}
else
{
// 启动旋转
ctrl.GetComponent<ObjectRotate>().enabled = false;
isRotating = true;
isPaused = false;
}
}
public void EndRotation()
{
// 结束旋转并将物体旋转归零
if (rotationTween != null)
{
rotationTween.Kill(); // 结束现有的 Tween
}
// 旋转归零,持续时间为 1 秒
rotationTween = transform.DORotate(Vector3.zero, 1f, RotateMode.FastBeyond360)
.OnKill(() =>
{
isRotating = false; // 停止旋转标志
isPaused = false; // 确保旋转不会暂停
});
}
}