1.dotween.to()延迟方法
DOTween.To(() => 0, a => { }, 0, 2).OnComplete(() =>
{
});
2.Dotween.Sequence()动画队列方法
Sequence sq = DOTween.Sequence();
sq.AppendInterval(1f)//延时
sq.Append()//添加动画
sq.Join()//插入动画
sq.AppendCallback()//添加动画播放完成后的回调
sq.JoinCallback()//插入动画播放完成后的回调
动画队列可以插入另一个动画队列从而形成更加复杂的动画
3.DoPath()路径轨道方法
transform.DOPath(路径点数组, 持续时间, 路径线条类型, 路径模式, 路径精度, 路径显示颜色)
.SetEase(Ease.InOutSine)//动画曲线
.SetLookAt(0.01f, Vector3.forward, Vector3.up)//位移看向方向
.SetOptions(false, AxisConstraint.Y, AxisConstraint.X | AxisConstraint.Z).OnComplete(() => { });//固定旋转轴,
3.1计算dopath曲线的切斜 使得物体一直看向切线方向
private void LookDirection(Vector3[] path4, float speed = 2)
{
// 获取当前路径的切线方向
Vector3 tangent = GetPathTangent(transform.position, path4);
if (tangent != Vector3.zero)
{
// 让物体朝向切线方向
Quaternion targetRotation = Quaternion.LookRotation(tangent);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * speed);
}
}
// 获取路径的切线方向
Vector3 GetPathTangent(Vector3 position, Vector3[] path)
{
// 找到最近的路径段
int segmentIndex = GetClosestSegmentIndex(position, path);
if (segmentIndex < 0 || segmentIndex >= path.Length - 1)
{
return Vector3.zero;
}
// 计算 Catmull-Rom 曲线的切线方向
Vector3 p0 = segmentIndex > 0 ? path[segmentIndex - 1] : path[segmentIndex];
Vector3 p1 = path[segmentIndex];
Vector3 p2 = path[segmentIndex + 1];
Vector3 p3 = segmentIndex < path.Length - 2 ? path[segmentIndex + 2] : path[segmentIndex + 1];
// 计算参数 t(当前点在路径段中的位置)
float t = GetTOnSegment(position, p1, p2);
// 返回切线方向
return GetCatmullRomTangent(p0, p1, p2, p3, t);
}
// 获取最近的路径段索引
int GetClosestSegmentIndex(Vector3 position, Vector3[] path)
{
int closestSegmentIndex = 0;
float closestDistance = float.MaxValue;
for (int i = 0; i < path.Length - 1; i++)
{
float distance = Vector3.Distance(position, path[i]);
if (distance < closestDistance)
{
closestDistance = distance;
closestSegmentIndex = i;
}
}
return closestSegmentIndex;
}
// 计算参数 t(当前点在路径段中的位置)
float GetTOnSegment(Vector3 position, Vector3 p1, Vector3 p2)
{
Vector3 segmentDirection = p2 - p1;
float segmentLength = segmentDirection.magnitude;
segmentDirection.Normalize();
Vector3 toPosition = position - p1;
float projection = Vector3.Dot(toPosition, segmentDirection);
return Mathf.Clamp01(projection / segmentLength);
}
// 计算 Catmull-Rom 曲线的切线方向
Vector3 GetCatmullRomTangent(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
{
// Catmull-Rom 曲线的导数公式
Vector3 tangent = 0.5f * (
(-p0 + p2) +
2f * (2f * p0 - 5f * p1 + 4f * p2 - p3) * t +
3f * (-p0 + 3f * p1 - 3f * p2 + p3) * t * t
);
return tangent.normalized; // 返回单位向量
}
4.DoJump()跳跃方法
transform.DOJump(目标位置,跳跃高度,跳跃次数,跳跃持续时间) 跳跃高度为0的时候就是直接曲线下落了,没有了上升弧度阶段
5.DoMove()位移方法
transform.DoMove(目标位置,位移时间)
6.DoRotate()旋转方法
transform.DoRotate(旋转角度,旋转时间)