FreeCameraMove.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace VolumetricFogAndMist {
  4. public class FreeCameraMove : MonoBehaviour {
  5. public float cameraSensitivity = 150;
  6. public float climbSpeed = 20;
  7. public float normalMoveSpeed = 20;
  8. public float slowMoveFactor = 0.25f;
  9. public float fastMoveFactor = 3;
  10. private float rotationX = 0.0f;
  11. private float rotationY = 0.0f;
  12. // void Start () {
  13. // Cursor.lockState = CursorLockMode.Locked;
  14. // }
  15. void Update () {
  16. Vector2 mousePos = Input.mousePosition;
  17. if (mousePos.x < 0 || mousePos.x > Screen.width || mousePos.y < 0 || mousePos.y > Screen.height)
  18. return;
  19. rotationX += Input.GetAxis ("Mouse X") * cameraSensitivity * Time.deltaTime;
  20. rotationY += Input.GetAxis ("Mouse Y") * cameraSensitivity * Time.deltaTime;
  21. rotationY = Mathf.Clamp (rotationY, -90, 90);
  22. transform.localRotation = Quaternion.AngleAxis (rotationX, Vector3.up);
  23. transform.localRotation *= Quaternion.AngleAxis (rotationY, Vector3.left);
  24. if (Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift)) {
  25. transform.position += transform.forward * (normalMoveSpeed * fastMoveFactor) * Input.GetAxis ("Vertical") * Time.deltaTime;
  26. transform.position += transform.right * (normalMoveSpeed * fastMoveFactor) * Input.GetAxis ("Horizontal") * Time.deltaTime;
  27. } else if (Input.GetKey (KeyCode.LeftControl) || Input.GetKey (KeyCode.RightControl)) {
  28. transform.position += transform.forward * (normalMoveSpeed * slowMoveFactor) * Input.GetAxis ("Vertical") * Time.deltaTime;
  29. transform.position += transform.right * (normalMoveSpeed * slowMoveFactor) * Input.GetAxis ("Horizontal") * Time.deltaTime;
  30. } else {
  31. transform.position += transform.forward * normalMoveSpeed * Input.GetAxis ("Vertical") * Time.deltaTime;
  32. transform.position += transform.right * normalMoveSpeed * Input.GetAxis ("Horizontal") * Time.deltaTime;
  33. }
  34. if (Input.GetKey (KeyCode.Q)) {
  35. transform.position -= transform.up * climbSpeed * Time.deltaTime;
  36. }
  37. if (Input.GetKey (KeyCode.E)) {
  38. transform.position += transform.up * climbSpeed * Time.deltaTime;
  39. }
  40. }
  41. }
  42. }