DNP_Camera.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace DamageNumbersPro.Demo
  5. {
  6. public class DNP_Camera : MonoBehaviour
  7. {
  8. // Instance
  9. public static DNP_Camera instance;
  10. // Shooting
  11. GameObject cubeHighlight;
  12. float nextShotTime;
  13. float nextRaycastTime;
  14. float lookTime;
  15. // Movement
  16. Vector3 velocity;
  17. void Awake()
  18. {
  19. instance = this;
  20. cubeHighlight = GameObject.Find("Special").transform.Find("Prefabs/Other/Cube Highlight").gameObject;
  21. }
  22. void Update()
  23. {
  24. HandleMovement();
  25. HandleShooting();
  26. // Escape
  27. if(DNP_InputHandler.GetEscape())
  28. {
  29. ShowMouse();
  30. Invoke("ShowMouse", 0.1f);
  31. Invoke("ShowMouse", 0.2f);
  32. Invoke("ShowMouse", 0.25f);
  33. Invoke("ShowMouse", 0.3f);
  34. CancelInvoke("HideMouse");
  35. }
  36. }
  37. void ShowMouse()
  38. {
  39. Cursor.visible = true;
  40. Cursor.lockState = CursorLockMode.None;
  41. }
  42. void HideMouse()
  43. {
  44. if(DNP_InputHandler.GetLeftHeld())
  45. {
  46. Invoke("HideMouse", 0.1f);
  47. }
  48. else
  49. {
  50. Cursor.visible = false;
  51. Cursor.lockState = CursorLockMode.Locked;
  52. }
  53. }
  54. void LateUpdate()
  55. {
  56. HandleLooking();
  57. }
  58. // Functions
  59. void HandleShooting()
  60. {
  61. if(DNP_InputHandler.GetLeftClick())
  62. {
  63. Shoot();
  64. nextShotTime = Time.time + 0.3f;
  65. } else if(DNP_InputHandler.GetRightHeld() && Time.time > nextShotTime)
  66. {
  67. Shoot();
  68. nextShotTime = Time.time + 0.06f;
  69. }
  70. // Detection
  71. if (Time.time > nextRaycastTime)
  72. {
  73. nextRaycastTime = Time.time + 0.11f;
  74. RaycastHit raycast;
  75. if (Physics.Raycast(transform.position, transform.forward, out raycast, 100))
  76. {
  77. DNP_Crosshair.targetEnemy = raycast.collider.gameObject.layer == 1;
  78. }
  79. }
  80. }
  81. void Shoot()
  82. {
  83. if (Cursor.visible)
  84. {
  85. if(!IsInvoking("HideMouse"))
  86. {
  87. Invoke("HideMouse", 0.1f);
  88. lookTime = Time.time + 0.3f;
  89. }
  90. return;
  91. }
  92. RaycastHit raycast;
  93. if(Physics.Raycast(transform.position, transform.forward, out raycast, 100))
  94. {
  95. // Create Damage Number
  96. DNP_PrefabSettings settings = DNP_DemoManager.instance.GetSettings();
  97. DamageNumber prefab = DNP_DemoManager.instance.GetCurrent();
  98. float number = 1 + Mathf.Pow(Random.value, 2.2f) * settings.numberRange;
  99. if(prefab.digitSettings.decimals == 0)
  100. {
  101. number = Mathf.Floor(number);
  102. }
  103. DamageNumber newDamageNumber = prefab.Spawn(raycast.point, number);
  104. // Apply Demo Settings
  105. settings.Apply(newDamageNumber);
  106. // Create Cube
  107. if (raycast.collider.gameObject.layer != 1)
  108. {
  109. Vector3 cubePosition = raycast.point - raycast.normal * 0.1f;
  110. cubePosition.x = Mathf.FloorToInt(cubePosition.x) + 0.5f;
  111. cubePosition.y = Mathf.FloorToInt(cubePosition.y) + 0.5f;
  112. cubePosition.z = Mathf.FloorToInt(cubePosition.z) + 0.5f;
  113. GameObject newCube = Instantiate<GameObject>(cubeHighlight);
  114. newCube.transform.position = cubePosition;
  115. newCube.SetActive(true);
  116. DNP_Crosshair.instance.HitWall();
  117. }
  118. else
  119. {
  120. DNP_Target target = raycast.collider.GetComponent<DNP_Target>();
  121. DNP_Crosshair.instance.HitTarget();
  122. if (target != null)
  123. {
  124. if(newDamageNumber.spamGroup != "")
  125. {
  126. newDamageNumber.spamGroup += target.GetInstanceID();
  127. }
  128. newDamageNumber.enableFollowing = true;
  129. newDamageNumber.followedTarget = target.transform;
  130. target.Hit();
  131. }
  132. }
  133. }
  134. }
  135. void HandleLooking()
  136. {
  137. if (Time.time < lookTime || Cursor.visible) return;
  138. Vector2 mouseDelta = DNP_InputHandler.GetMouseDelta();
  139. Vector3 eulerAngles = transform.eulerAngles;
  140. eulerAngles.y += mouseDelta.x;
  141. eulerAngles.x -= mouseDelta.y;
  142. if (eulerAngles.x > 180)
  143. {
  144. eulerAngles.x -= 360;
  145. }
  146. eulerAngles.x = Mathf.Clamp(eulerAngles.x, -80f, 80f);
  147. transform.eulerAngles = eulerAngles;
  148. }
  149. void HandleMovement()
  150. {
  151. Vector3 desiredDirection = Vector3.zero;
  152. if (DNP_InputHandler.GetRight())
  153. {
  154. desiredDirection.x += 1;
  155. }
  156. if (DNP_InputHandler.GetLeft())
  157. {
  158. desiredDirection.x += -1;
  159. }
  160. if (DNP_InputHandler.GetForward())
  161. {
  162. desiredDirection.z += 1;
  163. }
  164. if (DNP_InputHandler.GetBack())
  165. {
  166. desiredDirection.z += -1;
  167. }
  168. if (DNP_InputHandler.GetUp())
  169. {
  170. desiredDirection.y += 1;
  171. }
  172. if (DNP_InputHandler.GetDown())
  173. {
  174. desiredDirection.y += -1;
  175. }
  176. if(desiredDirection.magnitude > 0.1f)
  177. {
  178. desiredDirection.Normalize();
  179. }
  180. velocity = Vector3.Lerp(velocity, (desiredDirection.z * transform.forward + desiredDirection.x * transform.right + desiredDirection.y * Vector3.up) * 7f, Time.deltaTime * 6f);
  181. Vector3 position = transform.position;
  182. Vector3 clampedPosition = new Vector3(Mathf.Clamp(position.x, -4f, 4f), Mathf.Clamp(position.y, 1f, 6f), Mathf.Clamp(position.z, -12f, 4f));
  183. position = Vector3.Lerp(position, clampedPosition, Time.deltaTime * 15f) + velocity * Time.deltaTime;
  184. // Apply
  185. transform.position = position;
  186. }
  187. }
  188. }