Advertisement
Guest User

Untitled

a guest
Jul 11th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3.  
  4. public class Rocket : MonoBehaviour
  5. {
  6.  
  7. [SerializeField] float rcsThrust = 100f;
  8. [SerializeField] float mainThrust = 100f;
  9. [SerializeField] AudioClip mainEngine;
  10. [SerializeField] AudioClip success;
  11. [SerializeField] AudioClip death;
  12.  
  13. Rigidbody rigidBody;
  14. AudioSource audioSource;
  15.  
  16. enum State { Alive, Dying, Transcending }
  17. State state = State.Alive;
  18.  
  19.  
  20. // Start is called before the first frame update
  21. void Start()
  22. {
  23. rigidBody = GetComponent<Rigidbody>();
  24. audioSource = GetComponent<AudioSource>();
  25. }
  26.  
  27. // Update is called once per frame
  28. void Update()
  29. {
  30. if (state == State.Alive)
  31. RespondToRotateInput();
  32. RespondToThrustInput();
  33. }
  34.  
  35. void OnCollisionEnter(Collision collision)
  36. {
  37. if (state != State.Alive) {return;}
  38. switch (collision.gameObject.tag)
  39. {
  40. case "Friendly":
  41. break;
  42. case "Finish":
  43. StartSuccessSequence();
  44. break;
  45. default:
  46. StartDeathSequence();
  47. break;
  48. }
  49. }
  50. private void StartSuccessSequence()
  51. {
  52. state = State.Transcending;
  53. audioSource.Stop();
  54. audioSource.PlayOneShot(success);
  55. Invoke("LoadNextScene", 1f); //paramaterize this
  56. }
  57. private void StartDeathSequence()
  58. {
  59. state = State.Dying;
  60. audioSource.Stop();
  61. audioSource.PlayOneShot(death);
  62. Invoke("StartAgain", 1f);
  63. }
  64.  
  65.  
  66.  
  67. private void StartAgain()
  68. {
  69. SceneManager.LoadScene(0);
  70. }
  71.  
  72. private void LoadNextScene()
  73. {
  74. SceneManager.LoadScene(1);
  75. }
  76.  
  77. private void RespondToThrustInput()
  78. {
  79. if (Input.GetKey(KeyCode.Space)) // can thrust while rotating
  80. {
  81. ApplyThrust();
  82. }
  83. else
  84. {
  85. audioSource.Stop();
  86. }
  87. }
  88.  
  89. private void ApplyThrust()
  90. {
  91. rigidBody.AddRelativeForce(Vector3.up * mainThrust);
  92. if (!audioSource.isPlaying)
  93. {
  94. audioSource.PlayOneShot(mainEngine);
  95. }
  96.  
  97. }
  98. private void RespondToRotateInput()
  99. {
  100. rigidBody.freezeRotation = true; // take manual control
  101.  
  102.  
  103. float rotationThisFrame = rcsThrust * Time.deltaTime;
  104. if (Input.GetKey(KeyCode.A))
  105. {
  106.  
  107. transform.Rotate(Vector3.forward * rotationThisFrame);
  108. }
  109.  
  110. else if (Input.GetKey(KeyCode.D))
  111. {
  112.  
  113. transform.Rotate(-Vector3.forward * rotationThisFrame);
  114. }
  115. rigidBody.freezeRotation = false;
  116. }
  117.  
  118.  
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement