Vector2.Lerp(from, to, t) 当地三个参数=0时返回from, 当==1时返回to,0.5时返回1/2
//mRot会和new Vector2(x, y)无限接近 相等时不再移动
mRot = Vector2.Lerp(mRot, new Vector2(x, y), Time.deltaTime * 5f); Time.deltaTime * 5f是每帧的取值然后重新将大于mRot的值赋给mRot,最后mRot会跟new Vector2(x, y)相等
using UnityEngine;
public class TiltWindow : MonoBehaviour
{
public Vector2 range = new Vector2(5f, 3f);
Transform mTrans;
Quaternion mStart;
Vector2 mRot = Vector2.zero;
void Start ()
{
mTrans = transform;
mStart = mTrans.localRotation;
}
void Update ()
{
Vector3 pos = Input.mousePosition;
float halfWidth = Screen.width * 0.5f;
float halfHeight = Screen.height * 0.5f;
float x = Mathf.Clamp((pos.x - halfWidth) / halfWidth, -1f, 1f);
float y = Mathf.Clamp((pos.y - halfHeight) / halfHeight, -1f, 1f);
//mRot会和new Vector2(x, y)无限接近 相等时不再移动
mRot = Vector2.Lerp(mRot, new Vector2(x, y), Time.deltaTime * 5f);
mTrans.localRotation = mStart * Quaternion.Euler(-mRot.y * range.y, mRot.x * range.x, 0f);
}
}