1 /// <summary>
2 /// 在鼠标位置不同时 图片浮动效果
3 /// </summary>
4 public class TiltWindow : MonoBehaviour
5 {
6 public Vector2 range = new Vector2(5f, 3f);
7
8 Transform mTrans;
9 Quaternion mStart;
10 Vector2 mRot = Vector2.zero;
11
12 void Start ()
13 {
14 mTrans = transform;
15 mStart = mTrans.localRotation;
16 }
17
18 void Update ()
19 {
20 Vector3 pos = Input.mousePosition;
21
22 float halfWidth = Screen.width * 0.5f;
23 float halfHeight = Screen.height * 0.5f;
24 float x = Mathf.Clamp((pos.x - halfWidth) / halfWidth, -1f, 1f);
25 float y = Mathf.Clamp((pos.y - halfHeight) / halfHeight, -1f, 1f);
26 mRot = Vector2.Lerp(mRot, new Vector2(x, y), Time.deltaTime * 5f);
27
28 mTrans.localRotation = mStart * Quaternion.Euler(-mRot.y * range.y, mRot.x * range.x, 0f);
29 }
30 }