Unity3d使用DOTween实现动态路径方法DOPath和DOTweenPath无效情况
有个项目有如题的需求,主要是能给对象动态的设定路径,并执行延路径运动的动画。路径上的点是动态生成的,而且路径可能不唯一。
方案一、DOTweenPath
- 方案一、DOTweenPath
-
- 设定DOTweenPath的path然后DOPlay:
- 尝试打Log看看
- 设置path.wps 然后DOPlay
- 宣告DOTweenPath尝试失败
- 方案二、使用DOPath
- 总结
想当然的想到了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尝试失败原因还是不详,查了一圈也没找到答案,有知道的大佬可以分享一下。
方案二、使用DOPathusing 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不知道可不可行,目前测出的暂时没成功。