/// <summary>
/// 让一个UI随着鼠标的移动而进行rotation的变化
/// </summary>
public class MyTitleWindow : MonoBehaviour
{
public Vector2 range = new Vector2(20f, 20f);/// range:UI移动的范围
public Transform mTrans;/// mTrans:需要保持浮动的UI的父物体
private Vector2 mRot = Vector2.zero;/// mRot:移动中平滑运动的差值
private float moveSpeed = 5;//差值的速度,即到达旋转角度的速度
private float halfWidth = Screen.width * .5f;
private float halfHeight = Screen.height * .5f;
void Update()
{
float x = Mathf.Clamp((Input.mousePosition.x - halfWidth)/halfWidth, -1, 1);
float y = Mathf.Clamp((Input.mousePosition.y - halfHeight)/halfHeight, -1, 1);
mRot = Vector2.Lerp(mRot, new Vector2(x, y), Time.deltaTime * moveSpeed);
mTrans.localRotation = Quaternion.Euler(-mRot.y * range.y, mRot.x * range.x, 0f);
}
/// 让一个UI随着鼠标的移动而进行rotation的变化
/// </summary>
public class MyTitleWindow : MonoBehaviour
{
public Vector2 range = new Vector2(20f, 20f);/// range:UI移动的范围
public Transform mTrans;/// mTrans:需要保持浮动的UI的父物体
private Vector2 mRot = Vector2.zero;/// mRot:移动中平滑运动的差值
private float moveSpeed = 5;//差值的速度,即到达旋转角度的速度
private float halfWidth = Screen.width * .5f;
private float halfHeight = Screen.height * .5f;
void Update()
{
float x = Mathf.Clamp((Input.mousePosition.x - halfWidth)/halfWidth, -1, 1);
float y = Mathf.Clamp((Input.mousePosition.y - halfHeight)/halfHeight, -1, 1);
mRot = Vector2.Lerp(mRot, new Vector2(x, y), Time.deltaTime * moveSpeed);
mTrans.localRotation = Quaternion.Euler(-mRot.y * range.y, mRot.x * range.x, 0f);
}
}