您的位置:首页 > 移动开发 > Unity3D

Unity插件Dotween(二)

2017-06-27 18:15 615 查看
场景2

场景2的名称叫follow,场景中有两个cube,一个是可拖动的,另一个当可被拖动的cube坐标变化后会向它移动。



这个场景中的代码演示了:

怎么用变量保存下某段dotween动画

并且在后续对dotween动画进行目标值的修改

先说怎么用变量保存下某段动画,以便后续进行操作

其实我们书写的transform.DoMove()之类的代码最终都是返回了一个Tweener类型的变量

所以

Tweener tweener;
tweener = transform.DOMove(target.position, 1f).SetAutoKill(false);

这样就完成了声明和赋值。
setautokill的意思是不让这段动画在执行完毕后就kill掉,默认情况下一段dotween动画在播放完就会自动销毁,如果给某个物体挂上dotween animation组件,我们就会发现它默认是选择了auto play和auto kill的,既然是要保存下引用以便后续操作,所以自然是需要设置不自动销毁的。

上面两行代码,在start中书写,那么负责follow的cube就会移动向可拖动的cube,那么当可拖动的cube被拖动时,我们就需要重新的让follow的cube移动向新的一个坐标。

tweener.ChangeEndValue(target.position,true).Restart();changeendvalue方法,相当于修改了domove方法的第一个参数,true代表的是这段动画的目标值既然进行了更改,我是否以当前的坐标为起始点继续向目标点移动,选择true,那么我们会看到正常的结果,如果选择的是false,我们看到的结果就是follow的cube会回到执行dotween动画前的位置,然后再进行移动。
这个restart负责的是在更改值后让domove执行,没有它的话,如果动画结束了一次,你再移动它就不会跟上去了,这结果和上面不设置autokill为false一样。两者都是不可缺少的,否则这个案例就达不到想要的效果。

这是follow脚本的完整代码

using UnityEngine;
using System.Collections;
using DG.Tweening;

public class Follow : MonoBehaviour
{
public Transform target; // Target to follow
Vector3 targetLastPos;
Tweener tween;

void Start()
{
// First create the "move to target" tween and store it as a Tweener.
// In this case I'm also setting autoKill to FALSE so the tween can go on forever
// (otherwise it will stop executing if it reaches the target)
tween = transform.DOMove(target.position, 2).SetAutoKill(false);
// Store the target's last position, so it can be used to know if it changes
// (to prevent changing the tween if nothing actually changes)
targetLastPos = target.position;
}

void Update()
{
// Use an Update routine to change the tween's endValue each frame
// so that it updates to the target's position if that changed
if (targetLastPos == target.position) return;
// Add a Restart in the end, so that if the tween was completed it will play again
tween.ChangeEndValue(target.position, true).Restart();
targetLastPos = target.position;
}
}

对目标值进行修改之后,我遇到了一个需求,是对动画的执行速度进行修改。我不知道这两者有没有什么毛关系,但就是想记在一起。

事情是这样的,我做了一个旋转的物体,现在需要让它的转速是可调节的。并不要求精确的调节。

我首先弄出一个可以一直转动的物体

rotateTweener = rotateCube.DOLocalRotate(new Vector3(0, 180, 0), 180).SetSpeedBased(true).SetLoops(-1, LoopType.Incremental).SetEase(Ease.Linear);

通常旋转,位移,我们设置的都是endvalue和duration,它的速度就需要我们用当前值和目标值的差来除以时间计算出来,其实如果设置一下speedbase为true,那么我们原本作为动画周期的参数就会变成速度。比如这个转速就是180度每秒
setloops,设置的是动画的循环次数和循环模式,这在场景1中说过,-1代表无限次,incremental代表持续增加的,setease设置的是动画曲线,linear是线性的,代表一直匀速旋转。

关于其他动画曲线参数的效果,可以通过这个链接体验http://robertpenner.com/easing/easing_demo.html

怎么调整它旋转的速度,我一直在用...点出代码提示的方法寻找有可能的,最终找到了DoTimeScale方法可以实现我想要的功能,当然我并不知道这是不是最好的方法

if (Input.GetKeyDown(KeyCode.W))
{
Debug.Log(rotateTweener.Duration());
Debug.Log(rotateTweener.timeScale);
if (rotateTweener.timeScale < 2) {
rotateTweener.DOTimeScale(rotateTweener.timeScale*1.2f, 0.1f);
}

}

if (Input.GetKeyDown(KeyCode.S))
{
Debug.Log(rotateTweener.Duration());
Debug.Log(rotateTweener.timeScale);
if (rotateTweener.timeScale > 0.3)
{
rotateTweener.DOTimeScale(rotateTweener.timeScale * 0.8f, 0.1f);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: