TiltWindow.cs 700 B

1234567891011121314151617181920212223242526272829
  1. using UnityEngine;
  2. public class TiltWindow : MonoBehaviour
  3. {
  4. public Vector2 range = new Vector2(5f, 3f);
  5. Transform mTrans;
  6. Quaternion mStart;
  7. Vector2 mRot = Vector2.zero;
  8. void Start ()
  9. {
  10. mTrans = transform;
  11. mStart = mTrans.localRotation;
  12. }
  13. void Update ()
  14. {
  15. Vector3 pos = Input.mousePosition;
  16. float halfWidth = Screen.width * 0.5f;
  17. float halfHeight = Screen.height * 0.5f;
  18. float x = Mathf.Clamp((pos.x - halfWidth) / halfWidth, -1f, 1f);
  19. float y = Mathf.Clamp((pos.y - halfHeight) / halfHeight, -1f, 1f);
  20. mRot = Vector2.Lerp(mRot, new Vector2(x, y), Time.deltaTime * 5f);
  21. mTrans.localRotation = mStart * Quaternion.Euler(-mRot.y * range.y, mRot.x * range.x, 0f);
  22. }
  23. }