FlyCamera.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using UnityEngine;
  2. using System.Collections;
  3. public class FlyCamera : MonoBehaviour
  4. {
  5. /*
  6. Writen by Windexglow 11-13-10. Use it, edit it, steal it I don't care.
  7. Converted to C# 27-02-13 - no credit wanted.
  8. Simple flycam I made, since I couldn't find any others made public.
  9. Made simple to use (drag and drop, done) for regular keyboard layout
  10. wasd : basic movement
  11. shift : Makes camera accelerate
  12. space : Moves camera on X and Z axis only. So camera doesn't gain any height*/
  13. public float mainSpeed = 100.0f; //regular speed
  14. public float shiftAdd = 250.0f; //multiplied by how long shift is held. Basically running
  15. public float maxShift = 1000.0f; //Maximum speed when holdin gshift
  16. public float camSens = 0.25f; //How sensitive it with mouse
  17. public bool rotateOnlyIfMousedown = true;
  18. public bool movementStaysFlat = true;
  19. private Vector3 lastMouse = new Vector3(255, 255, 255); //kind of in the middle of the screen, rather than at the top (play)
  20. private float totalRun = 1.0f;
  21. void Update()
  22. {
  23. if (Input.GetMouseButtonDown(1))
  24. lastMouse = Input.mousePosition; // $CTK reset when we begin
  25. if (!rotateOnlyIfMousedown ||
  26. (rotateOnlyIfMousedown && Input.GetMouseButton(1)))
  27. {
  28. lastMouse = Input.mousePosition - lastMouse;
  29. lastMouse = new Vector3(-lastMouse.y * camSens, lastMouse.x * camSens, 0);
  30. lastMouse = new Vector3(transform.eulerAngles.x + lastMouse.x, transform.eulerAngles.y + lastMouse.y, 0);
  31. transform.eulerAngles = lastMouse;
  32. lastMouse = Input.mousePosition;
  33. //Mouse camera angle done.
  34. }
  35. //Keyboard commands
  36. Vector3 p = GetBaseInput();
  37. foreach (Touch touch in Input.touches)
  38. {
  39. if (touch.position.x < Screen.width / 2)
  40. {
  41. Touch moveTouch = touch;
  42. p = moveTouch.deltaPosition.normalized;
  43. }
  44. else
  45. {
  46. transform.eulerAngles = new Vector3(transform.eulerAngles.y+camSens*touch.deltaPosition.normalized.y,transform.eulerAngles.x + camSens*touch.deltaPosition.normalized.x, 0);
  47. }
  48. }
  49. if (Input.GetKey(KeyCode.LeftShift))
  50. {
  51. totalRun += Time.deltaTime;
  52. p = p * totalRun * shiftAdd;
  53. p.x = Mathf.Clamp(p.x, -maxShift, maxShift);
  54. p.y = Mathf.Clamp(p.y, -maxShift, maxShift);
  55. p.z = Mathf.Clamp(p.z, -maxShift, maxShift);
  56. }
  57. else
  58. {
  59. totalRun = Mathf.Clamp(totalRun * 0.5f, 1f, 1000f);
  60. p = p * mainSpeed;
  61. }
  62. p = p * Time.deltaTime;
  63. Vector3 newPosition = transform.position;
  64. if (Input.GetKey(KeyCode.Space)
  65. || (movementStaysFlat && !(rotateOnlyIfMousedown && Input.GetMouseButton(1))))
  66. {
  67. //If player wants to move on X and Z axis only
  68. transform.Translate(p);
  69. newPosition.x = transform.position.x;
  70. newPosition.z = transform.position.z;
  71. transform.position = newPosition;
  72. }
  73. else
  74. {
  75. transform.Translate(p);
  76. }
  77. }
  78. private Vector3 GetBaseInput()
  79. {
  80. //returns the basic values, if it's 0 than it's not active.
  81. Vector3 p_Velocity = new Vector3();
  82. if (Input.GetKey(KeyCode.W))
  83. {
  84. p_Velocity += new Vector3(0, 0, 100);
  85. }
  86. if (Input.GetKey(KeyCode.S))
  87. {
  88. p_Velocity += new Vector3(0, 0, -100);
  89. }
  90. if (Input.GetKey(KeyCode.A))
  91. {
  92. p_Velocity += new Vector3(-100, 0, 0);
  93. }
  94. if (Input.GetKey(KeyCode.D))
  95. {
  96. p_Velocity += new Vector3(100, 0, 0);
  97. }
  98. return p_Velocity;
  99. }
  100. }
  101. /*
  102. public class FingerTouch : MonoBehaviour{
  103. Vector2 touchStart;
  104. bool active;
  105. Vector2 direction;
  106. public int fingerId;
  107. private void Awake()
  108. {
  109. }
  110. public void Update()
  111. {
  112. foreach (Touch touch in Input.touches)
  113. {
  114. if (touch.phase == TouchPhase.Began)
  115. {
  116. active = true;
  117. touchStart = touch.position;
  118. }
  119. else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
  120. {
  121. //we stop the movement
  122. direction = Vector2.zero;
  123. active = false;
  124. }
  125. else if(touch.phase == TouchPhase.Moved)
  126. {
  127. direction = touch.position - touchStart;
  128. }
  129. }
  130. }
  131. }
  132. */