Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3.  
  4. public class Rocket : MonoBehaviour
  5. {
  6. [SerializeField] float rcsThrust = 100f;
  7. [SerializeField] float mainThrust = 100f;
  8. [SerializeField] float levelLoadDelay = 2f;
  9. [SerializeField] float loadCurrentLevel = 1f;
  10.  
  11. [SerializeField] AudioClip mainEngine;
  12. [SerializeField] AudioClip success;
  13. [SerializeField] AudioClip death;
  14.  
  15. [SerializeField] ParticleSystem mainEngineParticles;
  16. [SerializeField] ParticleSystem successParticles;
  17. [SerializeField] ParticleSystem deathParticles;
  18.  
  19. Rigidbody rigidBody;
  20. AudioSource audioSource;
  21.  
  22. bool isTransitioning = false;
  23. bool collisionsDisabled = false;
  24.  
  25. // Use this for initialization
  26. void Start()
  27. {
  28. rigidBody = GetComponent<Rigidbody>();
  29. audioSource = GetComponent<AudioSource>();
  30. }
  31.  
  32. // Update is called once per frame
  33. void Update()
  34. {
  35. if (!isTransitioning)
  36. {
  37. RespondToThrustInput();
  38. RespondToRotateInput();
  39. }
  40. if (Debug.isDebugBuild)
  41. {
  42. // todo only if debug on
  43. RespondToDebugKeys();
  44. }
  45. }
  46. private void RespondToDebugKeys()
  47. {
  48. if (Input.GetKeyDown(KeyCode.L))
  49. {
  50. LoadNextLevel();
  51. }
  52. else if (Input.GetKeyDown(KeyCode.C))
  53. {
  54. collisionsDisabled = !collisionsDisabled; // toggle
  55. }
  56. }
  57.  
  58.  
  59. void OnCollisionEnter(Collision collision)
  60. {
  61. if (isTransitioning || collisionsDisabled) { return; }
  62.  
  63. switch (collision.gameObject.tag)
  64. {
  65. case "Friendly":
  66. // do nothing
  67. break;
  68. case "Finish":
  69. StartSuccessSequence();
  70. break;
  71. default:
  72. StartDeathSequence();
  73. break;
  74. }
  75. }
  76.  
  77. private void StartSuccessSequence()
  78. {
  79. isTransitioning = true;
  80. audioSource.Stop();
  81. audioSource.PlayOneShot(success);
  82. successParticles.Play();
  83. Invoke("LoadCurrentLevel", levelLoadDelay);
  84.  
  85. }
  86.  
  87. private void StartDeathSequence()
  88. {
  89. isTransitioning = true;
  90. audioSource.Stop();
  91. audioSource.PlayOneShot(death);
  92. deathParticles.Play();
  93. Invoke("LoadCurrentLevel", levelLoadDelay);
  94. }
  95. private void LoadCurrentLevel()
  96. {
  97. int currentScene = SceneManager.GetActiveScene().buildIndex;
  98. SceneManager.LoadScene(currentScene);
  99. }
  100. private void LoadNextLevel()
  101. {
  102. int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
  103. int LoadCurrentLevel = currentSceneIndex + 1;
  104. if (LoadCurrentLevel == SceneManager.sceneCountInBuildSettings)
  105. {
  106. LoadCurrentLevel // loop back to level died in
  107. }
  108. SceneManager.LoadScene(nextSceneIndex);
  109. }
  110.  
  111. private void LoadFirstLevel()
  112. {
  113. SceneManager.LoadScene(LoadCurrentLevel);
  114. }
  115.  
  116. private void RespondToThrustInput()
  117. {
  118. if (Input.GetKey(KeyCode.Space)) // can thrust while rotating
  119. {
  120. ApplyThrust();
  121. }
  122. else
  123. {
  124. StopApplyingThrust();
  125. }
  126. }
  127.  
  128. private void StopApplyingThrust()
  129. {
  130. audioSource.Stop();
  131. mainEngineParticles.Stop();
  132. }
  133.  
  134. private void ApplyThrust()
  135. {
  136. rigidBody.AddRelativeForce(Vector3.up * mainThrust);
  137. if (!audioSource.isPlaying) // so it doesn't layer
  138. {
  139. audioSource.PlayOneShot(mainEngine);
  140. }
  141. mainEngineParticles.Play();
  142. }
  143.  
  144. private void RespondToRotateInput()
  145. {
  146. rigidBody.angularVelocity = Vector3.zero; // remove rotation due to pyhsics
  147.  
  148. float rotationThisFrame = rcsThrust * Time.deltaTime;
  149. if (Input.GetKey(KeyCode.A))
  150. {
  151. transform.Rotate(Vector3.forward * rotationThisFrame);
  152. }
  153. else if (Input.GetKey(KeyCode.D))
  154. {
  155. transform.Rotate(-Vector3.forward * rotationThisFrame);
  156. }
  157.  
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement