MouseLookAdvanced.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using UnityEngine;
  2. using System.Collections;
  3. [AddComponentMenu("Camera-Control/Mouse Look")]
  4. public class MouseLookAdvanced : 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 verticalAcceleration = 0f;
  14. float rotationX = 0F;
  15. float smoothRotationX = 0F;
  16. float rotationY = 0F;
  17. float smoothRotationY = 0F;
  18. Vector3 vMousePos;
  19. public float Speed = 100f;
  20. void Start()
  21. {
  22. rotationY = -transform.localEulerAngles.x;
  23. rotationX = transform.localEulerAngles.y;
  24. smoothRotationX = transform.localEulerAngles.y;
  25. smoothRotationY = -transform.localEulerAngles.x;
  26. }
  27. void Update()
  28. {
  29. verticalAcceleration = 0.0f;
  30. if (Input.GetMouseButtonDown (1))
  31. {
  32. if(Cursor.lockState == CursorLockMode.Locked)
  33. {
  34. Cursor.lockState = CursorLockMode.None;
  35. }
  36. else
  37. {
  38. Cursor.lockState = CursorLockMode.Locked;
  39. }
  40. Cursor.visible = !Cursor.visible;
  41. }
  42. if (Input.GetKey(KeyCode.Space)) { verticalAcceleration = 1.0f; }
  43. if (Input.GetKey(KeyCode.LeftShift)) { verticalAcceleration = -1.0f; }
  44. if(Cursor.lockState != CursorLockMode.Locked)
  45. {
  46. return;
  47. }
  48. rotationX += Input.GetAxis("Mouse X") * sensitivityX;
  49. rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
  50. rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
  51. // smooth mouse look
  52. smoothRotationX += (rotationX - smoothRotationX) * smoothSpeed * Time.smoothDeltaTime;
  53. smoothRotationY += (rotationY - smoothRotationY) * smoothSpeed * Time.smoothDeltaTime;
  54. // transform camera to new direction
  55. transform.localEulerAngles = new Vector3(-smoothRotationY, smoothRotationX, 0);
  56. // handle camera movement via controller
  57. Vector3 inputMag = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  58. Vector3 inputMoveDirection = transform.rotation * inputMag;
  59. transform.position += inputMoveDirection * Speed * Time.smoothDeltaTime;
  60. transform.position += new Vector3(0f, (Speed/2f) * verticalAcceleration * Time.smoothDeltaTime, 0);
  61. //transform.position += Vector3.up * (Input.GetAxis("VerticalOffset") * 10.0f * Time.smoothDeltaTime);
  62. transform.position += (transform.rotation * Vector3.forward) * Input.GetAxis("Mouse ScrollWheel") * 200.0f;
  63. }
  64. }