59 lines
1.6 KiB
C#
59 lines
1.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class QuadMoveCtr : MonoBehaviour
|
|
{
|
|
public Vector3 endPosition;
|
|
public float targetRotationY;
|
|
public float duration; // 移动持续时间(秒)
|
|
|
|
Vector3 startPoint;
|
|
|
|
Quaternion startRotation;
|
|
Quaternion endRotation;
|
|
|
|
private float elapsedTime = 0f;
|
|
private bool isMoving = false;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
startPoint = this.transform.localPosition;
|
|
|
|
startRotation = Quaternion.Euler( this.transform.localEulerAngles);
|
|
targetRotationY = targetRotationY % 360f;
|
|
if (targetRotationY < 0)
|
|
targetRotationY += 360f;
|
|
//Debug.Log("222startRotation:" + startRotation + "||" + this.transform.localEulerAngles);
|
|
endRotation = Quaternion.Euler(this.transform.localEulerAngles.x, targetRotationY, this.transform.localEulerAngles.z);
|
|
|
|
//Debug.Log("222endRotation:" + endRotation);
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (isMoving)
|
|
{
|
|
elapsedTime += Time.deltaTime;
|
|
float t = Mathf.Clamp01(elapsedTime / duration);
|
|
|
|
// 使用Lerp进行线性插值
|
|
transform.localPosition = Vector3.Lerp(startPoint, endPosition, t);
|
|
transform.localRotation = Quaternion.Lerp(startRotation, endRotation,t);
|
|
if (t >= 1f) // 移动完成
|
|
{
|
|
isMoving = false;
|
|
this.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void StartMove()
|
|
{
|
|
elapsedTime = 0;
|
|
isMoving = true;
|
|
}
|
|
}
|