65 lines
1.8 KiB
C#
65 lines
1.8 KiB
C#
|
|
|
|
using Script.RuntimeScript;
|
|
using System;
|
|
using UnityEngine;
|
|
|
|
public class MoveWithCurve : MonoBehaviour
|
|
{
|
|
[HideInInspector]
|
|
public Vector3 EndPoint = new Vector3();
|
|
DiggingMoveCurve curve;
|
|
|
|
private float _duration = 0.5f;
|
|
private float _elapsedTime = 0f;
|
|
private Vector3 _startPoint = Vector3.zero;
|
|
|
|
private bool _begin = false;
|
|
private Action _effectOver;
|
|
|
|
public void Begin(Action callBack)
|
|
{
|
|
_begin = true;
|
|
_elapsedTime = 0;
|
|
_effectOver = callBack;
|
|
}
|
|
|
|
public void SetStartPosition(Vector3 value, DiggingMoveCurve curve)
|
|
{
|
|
this.curve = curve;
|
|
_startPoint = value;
|
|
if (curve.CurveX.length > 0)
|
|
{
|
|
float startTime = curve.CurveX.keys[0].time;
|
|
float endTime = curve.CurveX.keys[curve.CurveX.length - 1].time;
|
|
_duration = endTime - startTime;
|
|
}
|
|
|
|
Update();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (!_begin || curve == null) return;
|
|
_elapsedTime += Time.deltaTime;
|
|
if (_elapsedTime > _duration)
|
|
{
|
|
_elapsedTime = _duration;
|
|
_begin = false;
|
|
_effectOver?.Invoke();
|
|
}
|
|
|
|
Vector3 l = Vector3.Lerp(_startPoint, EndPoint, _elapsedTime / _duration);
|
|
float speedY = curve.CurveSpeedY != null ? curve.CurveSpeedY.Evaluate(_elapsedTime) : 1;
|
|
float speedZ = curve.CurveSpeedZ != null ? curve.CurveSpeedZ.Evaluate(_elapsedTime) : 1;
|
|
// 使用曲线计算位置
|
|
|
|
float newX = curve.CurveX.Evaluate(_elapsedTime) * curve.RadianScale + l.x;
|
|
float newY = curve.CurveY.Evaluate(_elapsedTime) * curve.RadianScale * speedY + l.y;
|
|
float newZ = curve.CurveZ.Evaluate(_elapsedTime) * curve.RadianScale * speedZ + l.z;
|
|
transform.position = new Vector3(newX, newY, newZ);
|
|
}
|
|
|
|
|
|
}
|