ExtendedFlycam.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace TobyFredson
  4. {
  5. public class ExtendedFlycam : MonoBehaviour
  6. {
  7. /*
  8. EXTENDED FLYCAM
  9. Desi Quintans (CowfaceGames.com), 17 August 2012.
  10. Based on FlyThrough.js by Slin (http://wiki.unity3d.com/index.php/FlyThrough), 17 May 2011.
  11. LICENSE
  12. Free as in speech, and free as in beer.
  13. FEATURES
  14. WASD/Arrows: Movement
  15. Q: Climb
  16. E: Drop
  17. Shift: Move faster
  18. Control: Move slower
  19. End: Toggle cursor locking to screen (you can also press Ctrl+P to toggle play mode on and off).
  20. */
  21. public float cameraSensitivity = 3;
  22. public float climbSpeed = 4;
  23. public float normalMoveSpeed = 10;
  24. public float slowMoveFactor = 0.25f;
  25. public float fastMoveFactor = 3;
  26. private float rotationX = 0.0f;
  27. private float rotationY = 0.0f;
  28. void Start()
  29. {
  30. Cursor.lockState = CursorLockMode.Locked;
  31. }
  32. void Update()
  33. {
  34. rotationX += Input.GetAxis("Mouse X") * cameraSensitivity ;
  35. rotationY += Input.GetAxis("Mouse Y") * cameraSensitivity ;
  36. rotationY = Mathf.Clamp(rotationY, -90, 90);
  37. if (Mathf.Abs(Input.GetAxis("Mouse X")) > 0 || Mathf.Abs(Input.GetAxis("Mouse Y")) > 0)
  38. {
  39. transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
  40. transform.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);
  41. }
  42. if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
  43. {
  44. transform.position += transform.forward * (normalMoveSpeed * fastMoveFactor) * Input.GetAxis("Vertical") * Time.deltaTime;
  45. transform.position += transform.right * (normalMoveSpeed * fastMoveFactor) * Input.GetAxis("Horizontal") * Time.deltaTime;
  46. }
  47. else if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
  48. {
  49. transform.position += transform.forward * (normalMoveSpeed * slowMoveFactor) * Input.GetAxis("Vertical") * Time.deltaTime;
  50. transform.position += transform.right * (normalMoveSpeed * slowMoveFactor) * Input.GetAxis("Horizontal") * Time.deltaTime;
  51. }
  52. else
  53. {
  54. if (Mathf.Abs(Input.GetAxis("Vertical")) > 0 || Mathf.Abs(Input.GetAxis("Horizontal")) > 0)
  55. {
  56. transform.position += transform.forward * normalMoveSpeed * Input.GetAxis("Vertical") * Time.deltaTime;
  57. transform.position += transform.right * normalMoveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
  58. }
  59. }
  60. if (Input.GetKey(KeyCode.Q)) { transform.position += transform.up * climbSpeed * Time.deltaTime; }
  61. if (Input.GetKey(KeyCode.E)) { transform.position -= transform.up * climbSpeed * Time.deltaTime; }
  62. }
  63. }
  64. }