Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class Rocket : MonoBehaviour
- {
- [SerializeField] float rcsThrust = 100f;
- [SerializeField] float mainThrust = 100f;
- [SerializeField] AudioClip mainEngine;
- [SerializeField] AudioClip success;
- [SerializeField] AudioClip death;
- Rigidbody rigidBody;
- AudioSource audioSource;
- enum State { Alive, Dying, Transcending }
- State state = State.Alive;
- // Start is called before the first frame update
- void Start()
- {
- rigidBody = GetComponent<Rigidbody>();
- audioSource = GetComponent<AudioSource>();
- }
- // Update is called once per frame
- void Update()
- {
- if (state == State.Alive)
- RespondToRotateInput();
- RespondToThrustInput();
- }
- void OnCollisionEnter(Collision collision)
- {
- if (state != State.Alive) {return;}
- switch (collision.gameObject.tag)
- {
- case "Friendly":
- break;
- case "Finish":
- StartSuccessSequence();
- break;
- default:
- StartDeathSequence();
- break;
- }
- }
- private void StartSuccessSequence()
- {
- state = State.Transcending;
- audioSource.Stop();
- audioSource.PlayOneShot(success);
- Invoke("LoadNextScene", 1f); //paramaterize this
- }
- private void StartDeathSequence()
- {
- state = State.Dying;
- audioSource.Stop();
- audioSource.PlayOneShot(death);
- Invoke("StartAgain", 1f);
- }
- private void StartAgain()
- {
- SceneManager.LoadScene(0);
- }
- private void LoadNextScene()
- {
- SceneManager.LoadScene(1);
- }
- private void RespondToThrustInput()
- {
- if (Input.GetKey(KeyCode.Space)) // can thrust while rotating
- {
- ApplyThrust();
- }
- else
- {
- audioSource.Stop();
- }
- }
- private void ApplyThrust()
- {
- rigidBody.AddRelativeForce(Vector3.up * mainThrust);
- if (!audioSource.isPlaying)
- {
- audioSource.PlayOneShot(mainEngine);
- }
- }
- private void RespondToRotateInput()
- {
- rigidBody.freezeRotation = true; // take manual control
- float rotationThisFrame = rcsThrust * Time.deltaTime;
- if (Input.GetKey(KeyCode.A))
- {
- transform.Rotate(Vector3.forward * rotationThisFrame);
- }
- else if (Input.GetKey(KeyCode.D))
- {
- transform.Rotate(-Vector3.forward * rotationThisFrame);
- }
- rigidBody.freezeRotation = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement