MouseLook.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using UnityEngine;
  2. using System.Collections;
  3. [AddComponentMenu("Camera-Control/Mouse Look")]
  4. public class MouseLook : MonoBehaviour
  5. {
  6. public float sensitivityX = 5F;
  7. public float sensitivityY = 5F;
  8. public float minimumX = -360F;
  9. public float maximumX = 360F;
  10. public float minimumY = -90F;
  11. public float maximumY = 90F;
  12. public float smoothSpeed = 20F;
  13. float rotationX = 0F;
  14. float smoothRotationX = 0F;
  15. float rotationY = 0F;
  16. float smoothRotationY = 0F;
  17. Vector3 vMousePos;
  18. bool bActive = false;
  19. void Start()
  20. {
  21. vMousePos = Input.mousePosition;
  22. if (Screen.fullScreen)
  23. Cursor.visible = false;
  24. }
  25. void Update()
  26. {
  27. if (Input.GetMouseButton(1))
  28. {
  29. rotationX += Input.GetAxis("Mouse X") * sensitivityX;
  30. rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
  31. rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
  32. Cursor.visible = false;
  33. Screen.lockCursor = true;
  34. bActive = true;
  35. }
  36. else if (bActive || vMousePos != Input.mousePosition)
  37. {
  38. Cursor.visible = true;
  39. Screen.lockCursor = false;
  40. }
  41. vMousePos = Input.mousePosition;
  42. // smooth mouse look
  43. smoothRotationX += (rotationX - smoothRotationX) * smoothSpeed * Time.smoothDeltaTime;
  44. smoothRotationY += (rotationY - smoothRotationY) * smoothSpeed * Time.smoothDeltaTime;
  45. // transform camera to new direction
  46. transform.localEulerAngles = new Vector3(-smoothRotationY, smoothRotationX, 0);
  47. // handle camera movement via controller
  48. Vector3 inputMag = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  49. Vector3 inputMoveDirection = transform.rotation * inputMag;
  50. transform.position += inputMoveDirection * 25.0f * Time.smoothDeltaTime;
  51. //transform.position += Vector3.up * (Input.GetAxis("VerticalOffset") * 10.0f * Time.smoothDeltaTime);
  52. transform.position += (transform.rotation * Vector3.forward) * Input.GetAxis("Mouse ScrollWheel") * 200.0f;
  53. }
  54. }