Advertisement
Guest User

Untitled

a guest
Jul 30th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3.  
  4. public class HelicopterController : MonoBehaviour
  5. {
  6. public AudioSource HelicopterSound;
  7. public ControlPanel ControlPanel;
  8. public Rigidbody HelicopterModel;
  9. public HeliRotorController MainRotorController;
  10. public HeliRotorController SubRotorController;
  11.  
  12. public float TurnForce = 3f;
  13. public float ForwardForce = 10f;
  14. public float ForwardTiltForce = 20f;
  15. public float TurnTiltForce = 30f;
  16. public float EffectiveHeight = 100f;
  17.  
  18. public float turnTiltForcePercent = 1.5f;
  19. public float turnForcePercent = 1.3f;
  20.  
  21. private float _engineForce;
  22.  
  23. private Vector3 originalPlanePosition;
  24. private Quaternion originalPlaneRotation;
  25. public GameObject helicopterPlatform;
  26. public float lookAtSpeed;
  27.  
  28. public float EngineForce
  29. {
  30. get { return _engineForce; }
  31. set
  32. {
  33. MainRotorController.RotarSpeed = value * 80;
  34. SubRotorController.RotarSpeed = value * 40;
  35. HelicopterSound.pitch = Mathf.Clamp(value / 40, 0, 1.2f);
  36. if (UIGameController.runtime.EngineForceView != null)
  37. UIGameController.runtime.EngineForceView.text = string.Format("Engine value [ {0} ] ", (int)value);
  38.  
  39. _engineForce = value;
  40. }
  41. }
  42.  
  43. private Vector2 hMove = Vector2.zero;
  44. private Vector2 hTilt = Vector2.zero;
  45. private float hTurn = 0f;
  46. public bool IsOnGround = true;
  47.  
  48. // Use this for initialization
  49. void Start ()
  50. {
  51. originalPlanePosition = transform.position;
  52. originalPlaneRotation = transform.rotation;
  53.  
  54. ControlPanel.KeyPressed += OnKeyPressed;
  55. }
  56.  
  57. void Update () {
  58. }
  59.  
  60. void FixedUpdate()
  61. {
  62. LiftProcess();
  63. MoveProcess();
  64. TiltProcess();
  65. }
  66.  
  67. private void MoveProcess()
  68. {
  69. var turn = TurnForce * Mathf.Lerp(hMove.x, hMove.x * (turnTiltForcePercent - Mathf.Abs(hMove.y)), Mathf.Max(0f, hMove.y));
  70. hTurn = Mathf.Lerp(hTurn, turn, Time.fixedDeltaTime * TurnForce);
  71. HelicopterModel.AddRelativeTorque(0f, hTurn * HelicopterModel.mass, 0f);
  72. HelicopterModel.AddRelativeForce(Vector3.forward * Mathf.Max(0f, hMove.y * ForwardForce * HelicopterModel.mass));
  73. }
  74.  
  75. private void LiftProcess()
  76. {
  77. var upForce = 1 - Mathf.Clamp(HelicopterModel.transform.position.y / EffectiveHeight, 0, 1);
  78. upForce = Mathf.Lerp(0f, EngineForce, upForce) * HelicopterModel.mass;
  79. HelicopterModel.AddRelativeForce(Vector3.up * upForce);
  80. }
  81.  
  82. private void TiltProcess()
  83. {
  84. hTilt.x = Mathf.Lerp(hTilt.x, hMove.x * TurnTiltForce, Time.deltaTime);
  85. hTilt.y = Mathf.Lerp(hTilt.y, hMove.y * ForwardTiltForce, Time.deltaTime);
  86. HelicopterModel.transform.localRotation = Quaternion.Euler(hTilt.y, HelicopterModel.transform.localEulerAngles.y, -hTilt.x);
  87. }
  88.  
  89. private void OnKeyPressed(PressedKeyCode[] obj)
  90. {
  91. float tempY = 0;
  92. float tempX = 0;
  93.  
  94. // stable forward
  95. if (hMove.y > 0)
  96. tempY = - Time.fixedDeltaTime;
  97. else
  98. if (hMove.y < 0)
  99. tempY = Time.fixedDeltaTime;
  100.  
  101. // stable lurn
  102. if (hMove.x > 0)
  103. tempX = -Time.fixedDeltaTime;
  104. else
  105. if (hMove.x < 0)
  106. tempX = Time.fixedDeltaTime;
  107.  
  108.  
  109. foreach (var pressedKeyCode in obj)
  110. {
  111. switch (pressedKeyCode)
  112. {
  113. case PressedKeyCode.SpeedUpPressed:
  114.  
  115. EngineForce += 0.1f;
  116. break;
  117. case PressedKeyCode.SpeedDownPressed:
  118.  
  119. EngineForce -= 0.12f;
  120. if (EngineForce < 0) EngineForce = 0;
  121. break;
  122.  
  123. case PressedKeyCode.ForwardPressed:
  124.  
  125. if (IsOnGround) break;
  126. tempY = Time.fixedDeltaTime;
  127. break;
  128. case PressedKeyCode.BackPressed:
  129.  
  130. if (IsOnGround) break;
  131. tempY = -Time.fixedDeltaTime;
  132. break;
  133. case PressedKeyCode.LeftPressed:
  134.  
  135. if (IsOnGround) break;
  136. tempX = -Time.fixedDeltaTime;
  137. break;
  138. case PressedKeyCode.RightPressed:
  139.  
  140. if (IsOnGround) break;
  141. tempX = Time.fixedDeltaTime;
  142. break;
  143. case PressedKeyCode.TurnRightPressed:
  144. {
  145. if (IsOnGround) break;
  146. var force = (turnForcePercent - Mathf.Abs(hMove.y)) * HelicopterModel.mass;
  147. HelicopterModel.AddRelativeTorque(0f, force, 0);
  148. }
  149. break;
  150. case PressedKeyCode.TurnLeftPressed:
  151. {
  152. if (IsOnGround) break;
  153.  
  154. var force = -(turnForcePercent - Mathf.Abs(hMove.y)) * HelicopterModel.mass;
  155. HelicopterModel.AddRelativeTorque(0f, force, 0);
  156. }
  157. break;
  158. case PressedKeyCode.BackToBase:
  159. {
  160. Vector3 direction = helicopterPlatform.transform.position - transform.position;
  161. Quaternion toRotation = Quaternion.FromToRotation(transform.forward, direction);
  162. transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, lookAtSpeed * Time.time);
  163.  
  164. //transform.LookAt(helicopterPlatform.transform);
  165. }
  166. break;
  167. }
  168. }
  169.  
  170. hMove.x += tempX;
  171. hMove.x = Mathf.Clamp(hMove.x, -1, 1);
  172.  
  173. hMove.y += tempY;
  174. hMove.y = Mathf.Clamp(hMove.y, -1, 1);
  175.  
  176. }
  177.  
  178. private void OnCollisionEnter()
  179. {
  180. IsOnGround = true;
  181. }
  182.  
  183. private void OnCollisionExit()
  184. {
  185. IsOnGround = false;
  186. }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement