Player.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. 
  2. // =================================
  3. // Namespaces.
  4. // =================================
  5. using UnityEngine;
  6. // =================================
  7. // Define namespace.
  8. // =================================
  9. namespace MirzaBeig
  10. {
  11. namespace Demos
  12. {
  13. namespace TheLastParticle
  14. {
  15. // =================================
  16. // Classes.
  17. // =================================
  18. //[ExecuteInEditMode]
  19. [System.Serializable]
  20. public class Player : MonoBehaviour
  21. {
  22. // =================================
  23. // Nested classes and structures.
  24. // =================================
  25. // ...
  26. // =================================
  27. // Variables.
  28. // =================================
  29. // ...
  30. public Rigidbody rb { get; set; }
  31. // ...
  32. public float thrust = 0.125f;
  33. public float boostThrustMult = 2.0f;
  34. // ...
  35. new Camera camera;
  36. public float rotateToCameraForwardSpeed = Mathf.Infinity;
  37. // ...
  38. public float cameraFieldOfViewMaxSpeed = 20.0f;
  39. public Vector2 cameraFieldOfViewAngleRange = new Vector2(50.0f, 75.0f);
  40. // ...
  41. Vector3 startPosition;
  42. Quaternion startRotation;
  43. // =================================
  44. // Functions.
  45. // =================================
  46. // ...
  47. void Awake()
  48. {
  49. }
  50. // ...
  51. void Start()
  52. {
  53. rb = GetComponent<Rigidbody>();
  54. startPosition = transform.position;
  55. startRotation = transform.rotation;
  56. lockMouse();
  57. camera = Camera.main;
  58. }
  59. // ...
  60. void lockMouse()
  61. {
  62. Cursor.visible = false;
  63. Cursor.lockState = CursorLockMode.Locked;
  64. }
  65. void unlockMouse()
  66. {
  67. Cursor.visible = true;
  68. Cursor.lockState = CursorLockMode.None;
  69. }
  70. // ...
  71. void FixedUpdate()
  72. {
  73. // Input.
  74. float vertical = Input.GetAxis("Vertical");
  75. float horizontal = Input.GetAxis("Horizontal");
  76. //float forward = Input.GetKey(KeyCode.LeftShift) ? 1.0f : 0.0f;
  77. Vector3 force = Vector3.zero;
  78. force += Vector3.forward * vertical;
  79. force += Vector3.right * horizontal;
  80. force *= thrust;
  81. bool boost = Input.GetKey(KeyCode.LeftShift);
  82. if (boost)
  83. {
  84. force *= boostThrustMult;
  85. }
  86. rb.AddRelativeForce(force, ForceMode.VelocityChange);
  87. // ...
  88. float currentSpeed = rb.velocity.magnitude;
  89. float cameraFieldOfViewSpeedTime = currentSpeed / cameraFieldOfViewMaxSpeed;
  90. cameraFieldOfViewSpeedTime = Mathf.Clamp01(cameraFieldOfViewSpeedTime);
  91. camera.fieldOfView = Mathf.Lerp(
  92. cameraFieldOfViewAngleRange.x, cameraFieldOfViewAngleRange.y, cameraFieldOfViewSpeedTime);
  93. }
  94. // ...
  95. void Update()
  96. {
  97. if (Input.GetMouseButtonDown(0))
  98. {
  99. lockMouse();
  100. }
  101. else if (Input.GetKeyDown(KeyCode.Escape))
  102. {
  103. unlockMouse();
  104. }
  105. if (Input.GetKeyDown(KeyCode.R))
  106. {
  107. transform.position = startPosition;
  108. transform.rotation = startRotation;
  109. }
  110. }
  111. // ...
  112. void LateUpdate()
  113. {
  114. transform.rotation = Damping.dampQuaternion(transform.rotation,
  115. Quaternion.LookRotation(camera.transform.forward), rotateToCameraForwardSpeed, Time.deltaTime);
  116. }
  117. // =================================
  118. // End functions.
  119. // =================================
  120. }
  121. // =================================
  122. // End namespace.
  123. // =================================
  124. }
  125. }
  126. }
  127. // =================================
  128. // --END-- //
  129. // =================================