本文主要是介绍用Dotween做的一次数值折返运动,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
需求:求一个值单位时间内,从最大值X到0再返回到X
代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;public class 测试插值运动 : MonoBehaviour
{public float get;// Start is called before the first frame updatevoid Start(){float _max = 50;float _time = 2f;float _sp = 2f * _max / _time;DOTween.To(setter: value =>{get = TurnbackMovement(_max, value, _time, _sp);//Debug.Log(get);}, startValue: 0, endValue: _time, duration: _time).SetEase(Ease.Linear).OnComplete(() => { });}private float TurnbackMovement(float _max, float _currentTime, float _time,float _sp){var _num= 0f;if (_currentTime <= _time * 0.5f) _num = _max - _sp * _currentTime;else _num = _sp * (2 * _currentTime - _time);//Debug.LogError(_max+" "+_currentTime+" "+_time+" "+_sp+" "+ _num); return Mathf.Clamp(_num,0,_max);}
}
这篇关于用Dotween做的一次数值折返运动的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!