You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
8.5 KiB

5 years ago
  1. using UnityEngine;
  2. using System.Collections;
  3. public class ExplorationCamera : MonoBehaviour
  4. {
  5. //Turn
  6. [Range(1f, 50)]
  7. public float sensitivity = 30;
  8. [Range(.1f, 50)]
  9. public float smoothing = 5f;
  10. //Movement
  11. private float speedZ;
  12. private float speedX;
  13. private float speedY;
  14. [Range(.1f, 10)]
  15. public float Speed = .1f;
  16. [HideInInspector]
  17. public float InitialSpeed = .1f;
  18. [HideInInspector]
  19. public float FOV;
  20. private float initialFOV;
  21. Vector2 initialLook;
  22. Vector2 Look;
  23. private Vector3 MovementDirection;
  24. public float MaxAcceleration = 4;
  25. [Range(1f, 10)]
  26. public float AccelerationSmoothing = 10, SpeedSmooth = 1;
  27. Vector2 _smoothMouse;
  28. public bool HideCursor = false;
  29. float focalLength = 0, focalSize = 1;
  30. public bool ConstantMove = false;
  31. Camera ThisCamera;
  32. [HideInInspector]
  33. public float tilt;
  34. void Start()
  35. {
  36. try
  37. {
  38. //gamepad
  39. Input.GetAxis("LookHorizontal");
  40. }
  41. catch
  42. {
  43. print("Import the custom input to support gamepad in this camera script\n http://davidmiranda.me/files/FogVolume3/InputManager.asset");
  44. }
  45. //if (this.GetComponent<UnityStandardAssets.ImageEffects.DepthOfField>())
  46. //{
  47. // focalLength = this.GetComponent<UnityStandardAssets.ImageEffects.DepthOfField>().focalLength;
  48. // focalSize = this.GetComponent<UnityStandardAssets.ImageEffects.DepthOfField>().focalSize;
  49. //}
  50. }
  51. Vector2 lastPos;
  52. void OnGUI()
  53. {
  54. //ratón
  55. if (Event.current.type == EventType.MouseDown)
  56. {
  57. lastPos = Event.current.mousePosition;
  58. }
  59. else if (Event.current.type == EventType.MouseDrag || Event.current.type == EventType.MouseMove)
  60. {
  61. Vector3 diff = Event.current.mousePosition - lastPos;
  62. Look += new Vector2(diff.x * sensitivity / 50, -diff.y * sensitivity / 50);
  63. lastPos = Event.current.mousePosition;
  64. }
  65. }
  66. public float tiltSmoothing = 50, FOVTransitionSpeed = .1f;
  67. public float inclination = 13;
  68. public bool ApplyGravity;
  69. [Range(0f, 1)]
  70. public float gravityAccelerationScale = .1f;
  71. void OnDestroy()
  72. { Cursor.visible = true; }
  73. void FixedUpdate()
  74. {
  75. // if (Input.GetKey(KeyCode.Escape)) Application.Quit();
  76. try
  77. {
  78. //gamepad
  79. if (Mathf.Abs(Input.GetAxis("LookHorizontal")) > 0.017f || Mathf.Abs(Input.GetAxis("LookVertical")) > 0.017f)
  80. {
  81. Look += new Vector2(Input.GetAxis("LookHorizontal"), -Input.GetAxis("LookVertical")) * sensitivity;
  82. if (Input.GetAxis("LookHorizontal") > 0)
  83. tilt = Mathf.Lerp(tilt, -inclination * Mathf.Abs(Input.GetAxis("LookHorizontal")) * sensitivity, 1f / tiltSmoothing);
  84. else
  85. tilt = Mathf.Lerp(tilt, inclination * Mathf.Abs(Input.GetAxis("LookHorizontal")) * sensitivity, 1f / tiltSmoothing);
  86. }
  87. }
  88. catch
  89. {
  90. //
  91. }
  92. tilt = Mathf.Lerp(tilt, 0, 1f / tiltSmoothing);
  93. //DOF
  94. if (Input.GetKey(KeyCode.PageDown))
  95. focalLength -= .05f;
  96. if (Input.GetKey(KeyCode.PageUp))
  97. focalLength += .05f;
  98. if (Input.GetKey(KeyCode.End))
  99. focalSize -= .05f;
  100. if (Input.GetKey(KeyCode.Home))
  101. focalSize += .05f;
  102. //if (this.GetComponent<UnityStandardAssets.ImageEffects.DepthOfField>())
  103. //{
  104. // this.GetComponent<UnityStandardAssets.ImageEffects.DepthOfField>().focalLength = focalLength;
  105. // this.GetComponent<UnityStandardAssets.ImageEffects.DepthOfField>().focalSize = focalSize;
  106. //}
  107. //Turn interpolation
  108. _smoothMouse.x = Mathf.Lerp(_smoothMouse.x, Look.x, 1f / smoothing);
  109. _smoothMouse.y = Mathf.Lerp(_smoothMouse.y, Look.y, 1f / smoothing);
  110. //Aplica giro suave
  111. transform.localEulerAngles = new Vector3(-_smoothMouse.y, _smoothMouse.x, tilt);
  112. //Traslación:
  113. if (Input.GetKey(KeyCode.W) || Input.GetAxis("Vertical") > 0 || ConstantMove)
  114. {
  115. MovementDirection = new Vector3(0, 0, 1);
  116. speedZ = Mathf.Lerp(speedZ, Speed, 1f / smoothing);
  117. }
  118. if (Input.GetKey(KeyCode.S) || Input.GetAxis("Vertical") < 0)
  119. {
  120. MovementDirection = new Vector3(0, 0, -1);
  121. speedZ = Mathf.Lerp(speedZ, -Speed, 1f / smoothing);
  122. }
  123. if (Input.GetKey(KeyCode.A) || Input.GetAxis("Horizontal") < 0)
  124. {
  125. MovementDirection = new Vector3(-1, 0, 0);
  126. speedX = Mathf.Lerp(speedX, -Speed, 1f / smoothing);
  127. }
  128. if (Input.GetKey(KeyCode.D) || Input.GetAxis("Horizontal") > 0)
  129. {
  130. MovementDirection = new Vector3(1, 0, 0);
  131. speedX = Mathf.Lerp(speedX, Speed, 1f / smoothing);
  132. }
  133. if (Input.GetKey(KeyCode.Q) || Input.GetKey(KeyCode.JoystickButton4))
  134. {
  135. MovementDirection = new Vector3(0, -1, 0);
  136. speedY = Mathf.Lerp(speedY, -Speed, 1f / smoothing);
  137. }
  138. if (Input.GetKey(KeyCode.E) || Input.GetKey(KeyCode.JoystickButton5))
  139. {
  140. MovementDirection = new Vector3(0, 1, 0);
  141. speedY = Mathf.Lerp(speedY, Speed, 1f / smoothing);
  142. }
  143. //desaceleración
  144. speedZ = Mathf.Lerp(speedZ, 0, 1f / smoothing);
  145. MovementDirection.z = speedZ;
  146. speedY = Mathf.Lerp(speedY, 0, 1f / smoothing);
  147. MovementDirection.y = speedY;
  148. speedX = Mathf.Lerp(speedX, 0, 1f / smoothing);
  149. MovementDirection = new Vector3(speedX, speedY, speedZ);
  150. if (ApplyGravity)
  151. {
  152. MovementDirection += Physics.gravity * gravityAccelerationScale;
  153. }
  154. transform.Translate(MovementDirection);
  155. // transform.Rotate(Vector3.forward, tilt * 150);
  156. //Speed Ratón
  157. Speed = Mathf.Clamp(Speed, .001f, 100f);
  158. if (Input.GetMouseButton(1))
  159. {
  160. InitialSpeed += Input.GetAxis("Mouse ScrollWheel") * .5f;
  161. InitialSpeed = Mathf.Max(.1f, InitialSpeed);
  162. }
  163. //FOV
  164. if (Input.GetMouseButton(1) & Input.GetKey(KeyCode.C) & FOV > 5)
  165. {
  166. FOV -= 1;
  167. }
  168. if (Input.GetMouseButton(1) & Input.GetKey(KeyCode.Z) & FOV < 120)
  169. {
  170. FOV += 1;
  171. }
  172. //Al soltar la tecla:
  173. if (Input.GetMouseButton(1) == false)
  174. {
  175. FOV = Mathf.Lerp(FOV, initialFOV, FOVTransitionSpeed);//El último valor sería la velocidad a la que vuelve al fov original
  176. //Rueda:
  177. // speedZ += Input.GetAxis("Mouse ScrollWheel");
  178. }
  179. //Aplicar
  180. //
  181. //
  182. if (ThisCamera)
  183. if (ThisCamera.stereoEnabled == false)
  184. GetComponent<Camera>().fieldOfView = FOV;
  185. }
  186. float TriggerValue = 0;
  187. void Update()
  188. {
  189. //Extra velocidad:
  190. try
  191. {
  192. TriggerValue = Mathf.Pow(Input.GetAxis("FasterCamera") * 1 / AccelerationSmoothing + 1, 2);
  193. }
  194. catch { }
  195. //print(Speed);
  196. if (Input.GetKeyDown(KeyCode.LeftShift))
  197. {
  198. Speed *= MaxAcceleration;
  199. }
  200. try
  201. {
  202. if (Input.GetAxis("FasterCamera") > 0 && Speed < InitialSpeed * MaxAcceleration)
  203. {
  204. Speed *= TriggerValue;
  205. }
  206. else
  207. if (!Input.GetKey(KeyCode.LeftShift))
  208. Speed = Mathf.Lerp(Speed, InitialSpeed, 1 / AccelerationSmoothing);
  209. }
  210. catch
  211. {
  212. if (!Input.GetKey(KeyCode.LeftShift))
  213. Speed = Mathf.Lerp(Speed, InitialSpeed, 1 / AccelerationSmoothing);
  214. }
  215. // print(Speed);
  216. if (Input.GetKeyUp(KeyCode.LeftShift))
  217. {
  218. Speed /= MaxAcceleration;
  219. }
  220. }
  221. void OnEnable()
  222. {
  223. if (GetComponent<Camera>())
  224. ThisCamera = GetComponent<Camera>();
  225. InitialSpeed = Speed;
  226. if (HideCursor)
  227. Cursor.visible = false;
  228. if(ThisCamera)
  229. FOV = this.gameObject.GetComponent<Camera>().fieldOfView;
  230. initialFOV = FOV;
  231. initialLook = new Vector2(transform.eulerAngles.y, transform.eulerAngles.x * -1);
  232. // Debug.Log (transform.eulerAngles);
  233. Look = initialLook;
  234. _smoothMouse = initialLook;
  235. }
  236. }