您当前的位置: 首页 >  unity

十幺卜入

暂无认证

  • 4浏览

    0关注

    119博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity3d使用DOTween实现动态路径方法DOPath和DOTweenPath无效情况

十幺卜入 发布时间:2021-01-21 14:52:28 ,浏览量:4

Unity3d使用DOTween实现动态路径方法DOPath和DOTweenPath无效情况
  • 方案一、DOTweenPath
    • 设定DOTweenPath的path然后DOPlay:
    • 尝试打Log看看
    • 设置path.wps 然后DOPlay
    • 宣告DOTweenPath尝试失败
  • 方案二、使用DOPath
  • 总结
有个项目有如题的需求,主要是能给对象动态的设定路径,并执行延路径运动的动画。路径上的点是动态生成的,而且路径可能不唯一。

方案一、DOTweenPath

想当然的想到了DOTweenPath,路径和路径点是可以预编辑的,想着在运行中动态的设置路径就能完成。

设定DOTweenPath的path然后DOPlay:
using UnityEngine;
using DG.Tweening;
public class PathTest : MonoBehaviour
{
    public DOTweenPath dopath;
    Vector3[] wps = new Vector3[3] { new Vector3(0, 0, 0), new Vector3(3, 0, 0), new Vector3(3, 0, 3) };
    void Update()
    {
        if (Input.GetKeyUp(KeyCode.D)) 
            DoMovePath();
    }

    void DoMovePath() {
        dopath.path = new DG.Tweening.Plugins.Core.PathCore.Path(PathType.Linear, wps, 1);
        dopath.DOPlay();
    }
}

然而对象根本不会动,原因不详。

尝试打Log看看
void Update()
    {
        if (Input.GetKeyUp(KeyCode.D)) {
            DoMovePath();
            Debug.Log("按了D键");
        }
    }

    void DoMovePath() {
        dopath.path = new DG.Tweening.Plugins.Core.PathCore.Path(PathType.Linear, wps, 1);
        dopath.DOPlay();
        Debug.Log("执行了DoMovePath");
}

在这里插入图片描述

Log已输出,还是不原因。

设置path.wps 然后DOPlay

发现可以直接设置path的wps即路径的点。

void DoMovePath() {
        dopath.path.wps = wps;
        dopath.DOPlay();
        Debug.Log("执行了DoMovePath");
    }

结果也一样

宣告DOTweenPath尝试失败

原因还是不详,查了一圈也没找到答案,有知道的大佬可以分享一下。

方案二、使用DOPath
using UnityEngine;
using DG.Tweening;
public class PathTest : MonoBehaviour
{
    public DOTweenPath dopath;
    Vector3[] wps = new Vector3[3] { new Vector3(0, 0, 0), new Vector3(3, 0, 0), new Vector3(3, 0, 3) };
    void Update()
    {
        if (Input.GetKeyUp(KeyCode.D)) {
            DoMovePath();
            Debug.Log("按了D键");
        }
    }

    void DoMovePath() {
        //dopath.path = new DG.Tweening.Plugins.Core.PathCore.Path(PathType.Linear, wps, 1);
        //dopath.path.wps = wps;
        //dopath.DOPlay();
        transform.DOPath(wps, 3f);
        Debug.Log("执行了DoMovePath");
    }
}

嘿`````,完美运行了!!

在这里插入图片描述

总结

DOPath可以直接设置路径点和动画时间,实现执行动态路径的动画。 DOTweenPath不知道可不可行,目前测出的暂时没成功。

关注
打赏
1663314737
查看更多评论
立即登录/注册

微信扫码登录

0.1074s