/Assets/Scripts/WrongPipeline/PlayerCamera.cs

https://bitbucket.org/katas94/frustum-tessitura · C# · 157 lines · 120 code · 29 blank · 8 comment · 15 complexity · c7b197ea61c6a1a20d00029aa7e4f9f5 MD5 · raw file

  1. using UnityEngine;
  2. using System.Collections;
  3. namespace WrongPipeline
  4. {
  5. [DisallowMultipleComponent]
  6. [RequireComponent(typeof(Camera))]
  7. public class PlayerCamera : MonoBehaviour
  8. {
  9. public Player player;
  10. public Transform lookAt;
  11. public Transform target;
  12. public float distance = 5.0F;
  13. public float positionSmoothTime = 0.3F;
  14. public float rotationSmoothTime = 0.05F;
  15. public float planetChangeSmoothTime = 0.8F;
  16. public float sensitivity = 1;
  17. [HideInInspector]
  18. public bool planetChanged = false;
  19. private Vector3 rotation;
  20. private Vector3 offsetPosition;
  21. private int ignoreLayers;
  22. public void SetOnPlayer ()
  23. {
  24. rotation.z = 0.0F;
  25. rotation.x = 70; rotation.y = 0;
  26. offsetPosition = Quaternion.Euler(70, 0, 0) * Vector3.back * distance;
  27. target.position = lookAt.position + player.transform.TransformVector(offsetPosition);
  28. target.LookAt(lookAt, player.transform.up);
  29. if (gameObject.activeInHierarchy)
  30. {
  31. StopAllCoroutines();
  32. StartCoroutine(C_FollowTarget());
  33. }
  34. }
  35. public void SetChatched ()
  36. {
  37. Cursor.visible = false;
  38. Cursor.lockState = CursorLockMode.Locked;
  39. catched = true;
  40. }
  41. void OnEnable()
  42. {
  43. StopAllCoroutines();
  44. StartCoroutine(C_FollowTarget());
  45. }
  46. void Start ()
  47. {
  48. ignoreLayers = ~(LayerMask.NameToLayer("Player") | LayerMask.NameToLayer("PlanetFields"));
  49. SetOnPlayer();
  50. }
  51. private bool catched = false;
  52. void Update ()
  53. {
  54. // calculamos la rotacion que aplicaremos a la camara segun el movimiento del aton
  55. Vector3 deltaRotation = new Vector3(-Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X")) * sensitivity;
  56. if (!catched)
  57. {
  58. deltaRotation = Vector3.zero;
  59. }
  60. if (rotation.x + deltaRotation.x < -89 || rotation.x + deltaRotation.x > 89)
  61. deltaRotation.x = 0.0F;
  62. // aplicamos la rotacion calculada
  63. rotation += deltaRotation;
  64. // ajustamos l rotation dentro de unos limites
  65. // rotation.x = Mathf.Clamp(rotation.x, -89, 89);
  66. rotation.y -= rotation.y > 360 ? 360 : 0;
  67. rotation.y += rotation.y < -360 ? 360 : 0;
  68. // offsetPosition = Quaternion.Euler(rotation) * (Vector3.back * distance);
  69. // offsetPosition = player.transform.TransformVector(offsetPosition);
  70. player.transform.rotation = player.transform.rotation * Quaternion.Euler(0, deltaRotation.y, 0);
  71. deltaRotation.y = 0.0F;
  72. offsetPosition = Quaternion.Euler(deltaRotation) * offsetPosition;
  73. // player.cameraRotation = Quaternion.Euler(0, deltaRotation.y, 0) * player.cameraRotation;
  74. // player.cameraRotation = Quaternion.FromToRotation(player.transform.forward, Vector3.ProjectOnPlane(-offsetPosition, player.transform.up));
  75. RaycastHit hit;
  76. Vector3 direction = player.transform.TransformVector(offsetPosition);
  77. if (Physics.Raycast(lookAt.position, direction, out hit, distance, ignoreLayers) && !hit.collider.isTrigger && hit.collider.GetComponent<MeshRenderer>() != null)
  78. direction = direction.normalized * (hit.distance - 0.5F);
  79. target.position = lookAt.position + direction;
  80. target.LookAt(lookAt, player.transform.up);
  81. if (Input.GetMouseButtonDown(0))
  82. {
  83. Cursor.visible = false;
  84. Cursor.lockState = CursorLockMode.Locked;
  85. catched = true;
  86. }
  87. if (catched && Input.GetKeyDown(KeyCode.Escape))
  88. {
  89. Cursor.visible = true;
  90. Cursor.lockState = CursorLockMode.None;
  91. catched = false;
  92. }
  93. }
  94. private IEnumerator C_FollowTarget ()
  95. {
  96. Vector3 currentVelocity = Vector3.zero;
  97. Vector3 lookAtVelocity = Vector3.zero;
  98. Vector3 upVelocity = Vector3.zero;
  99. Vector3 currentForward = transform.forward;
  100. Vector3 currentUp = transform.up;
  101. while (true)
  102. {
  103. float smoothTime = planetChanged ? planetChangeSmoothTime : rotationSmoothTime;
  104. transform.position = Vector3.SmoothDamp(transform.position, target.position, ref currentVelocity, positionSmoothTime);
  105. currentForward= Vector3.SmoothDamp(currentForward, target.forward, ref lookAtVelocity, smoothTime);
  106. currentUp= Vector3.SmoothDamp(currentUp, target.up, ref upVelocity, smoothTime);
  107. transform.LookAt(transform.position + currentForward, currentUp);
  108. yield return null;
  109. }
  110. }
  111. private Coroutine planetChangedC;
  112. public void PlanetChanged ()
  113. {
  114. if (gameObject.activeInHierarchy)
  115. {
  116. if (planetChangedC != null)
  117. StopCoroutine(planetChangedC);
  118. planetChangedC = StartCoroutine(C_PlanetChange());
  119. }
  120. }
  121. private IEnumerator C_PlanetChange ()
  122. {
  123. planetChanged = true;
  124. yield return new WaitWhile(() => Vector3.Angle(transform.up, target.up) > 0.05F);
  125. planetChanged = false;
  126. }
  127. }
  128. }