DOTween插件DOPath()方法制作自定义导航路径

DOTween插件DOPath()自定义路径:

Transform位置为路径点位

方法1:

public Transform[] pathNodes;  // 路径节点数组

private void Start()
{
    // 创建路径
    Vector3[] path = new Vector3[pathNodes.Length];
    for (int i = 0; i < pathNodes.Length; i++)
    {
        path[i] = pathNodes[i].position;
    }

    // 使用DOTweenPath进行寻路动画
/*
*
*/
    transform.DOPath(path, 5f, PathType.Linear, PathMode.Full3D)
        .SetOptions(false)
        .SetLookAt(0.01f)    //物体看向路径
        .SetEase(Ease.Linear);
}

 方法2:


public Transform[] t;
    void Start()
    {
        //路线转换b把Transform转为Vetor3类型
         var positions = t.Select(u => u.position).ToArray();
        //OnOnComplete()回调效果,用于动画结束时触发
        transform.DOPath(positions, 20, PathType.CatmullRom, PathMode.Full3D, 10, Color.yellow).SetOptions(false).OnComplete(Ro);

    }


#优化上述脚本

    public void Mover(Transform[] tf)
    {
        //路线转换b把Transform[]转为Vetor3[]类型
        var positions = tf.Select(u => u.position).ToArray();
        //OnOnComplete()回调效果,用于动画结束时触发
        /*
         * postions点位
         * 第二个参数:完成的时间
         * PathType.CatmullRom  //设置路径为曲线
         * .SetEase (Ease.Linear)  //设置运动状态为匀速运动
         * .SetLookAt(centerPoint)  //设置标题字体一直看向大树
         * SetOptions(true);   //设置运动路径为闭环,即头尾相连
         * Color.yellow 路径颜色
         */
         transform.DOPath(positions, 10, PathType.CatmullRom, PathMode.Full3D, 1, Color.yellow).SetOptions(false).SetEase(Ease.Linear).OnComplete(Ro);
        
    }
    public void Ro()
    {
       Debug.Log("调用方法Ro");
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DOPathDotween中用于控制物体路径运动的函数之一,可以使物体沿着指定的路径进行运动。 下面是DOPath函数的基本用法: ```csharp // 2D路径运动 transform.DOPath(Vector3[] path, float duration, PathType pathType = PathType.Linear, PathMode pathMode = PathMode.TopDown2D, int resolution = 10, Color? gizmoColor = null); // 3D路径运动 transform.DOPath(Vector3[] path, float duration, PathType pathType = PathType.Linear, PathMode pathMode = PathMode.Full3D, int resolution = 10, Color? gizmoColor = null); ``` 参数解释: - `path`:路径点的数组,可以是二维或三维数组,表示物体的运动路径。 - `duration`:路径运动的持续时间。 - `pathType`:路径类型,可选值为:线性(`PathType.Linear`)、Bezier曲线(`PathType.CubicBezier`)、Catmull-Rom曲线(`PathType.CatmullRom`)和插值曲线(`PathType.Spline`)等。 - `pathMode`:路径模式,可选值为:2D(`PathMode.TopDown2D`)和3D(`PathMode.Full3D`)。 - `resolution`:路径分辨率,用于控制曲线的平滑度,数值越大曲线越平滑,但是运算量也越大。 - `gizmoColor`:路径的颜色,可用于在编辑器中显示路径。 下面是一个示例代码,展示了如何使用DOPath函数来控制物体沿着弧形路径进行运动: ```csharp using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class ArcPath : MonoBehaviour { public Transform target; public float duration = 2f; public Vector3[] pathPoints; void Start() { // 设置路径点 pathPoints = new Vector3[] { new Vector3(-2f, 0f, 0f), new Vector3(0f, 2f, 0f), new Vector3(2f, 0f, 0f) }; // 沿着Bezier曲线进行运动 target.DOPath(pathPoints, duration, PathType.CubicBezier); } } ``` 在上面的代码中,我们首先定义了一个路径点数组,然后在Start函数中使用DOPath函数来控制物体沿着Bezier曲线进行运动。由于我们没有指定路径模式,因此默认使用2D路径模式来进行运动。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值