更漂亮的位移指的是先慢再快再慢的这种位移,更像现实中的位移。也叫平滑阻尼。
这里有两种实现方式:
1.通过计算值:调的函数是Mathf.SmoothStep
例:
float t = 0;
// Update is called once per frame
void Update()
{
if(sprite.fillAmount != 1 && transform.gameObject.activeSelf)
{
t += Time.deltaTime/5;
if (m_startAction)
{
sprite.fillAmount = Mathf.SmoothStep(sprite.fillAmount, 1, t);
}
else
{
sprite.fillAmount = 1;
}
}
}
public void PerformAction()
{
m_startAction = true;
}
2.通过向量Vector3:调的函数是Vector3.SmoothDamp
例:
if (_buttonUp)
{
AddingFriendButton.transform.localPosition = Vector3.SmoothDamp(AddingFriendButton.transform.localPosition, _tabOriginalPosition, ref _velocity, 0.2f);
if (Vector3.Distance(AddingFriendButton.transform.localPosition, _tabOriginalPosition) < 0.01)
_buttonUp = false;
}
3.通过向量Vector3,设定持续时间,
Vector3 org = new Vector3();
Vector3 vec = new Vector3();
Vector3 _velocity = new Vector3();
public void RotateCameraFromGesture(Vector3 delta)
{
org = SceneCameraGameObject.transform.localEulerAngles;
vec = org;
vec.y += delta.x * 5;
vec.x += delta.y * -5;
vec.z = 0;
vec = Vector3.SmoothDamp(org, vec, ref _velocity, 1f);
if (vec.x < 0)
vec.x = 0;
if (vec.x > 45)
vec.x = 45;
SceneCameraGameObject.transform.localEulerAngles = vec;
//SceneCamera.transform.Rotate(new Vector3(-delta.y * 0.1f, 0, delta.x));//这个根据世界坐标转,上下左右转就出问题了
}