Multiple sounds

using UnityEngine.SceneManagement;
using UnityEngine;
using System;

public class Rocket : MonoBehaviour
{
enum State {Alive, Dying, Transcending};
State state = State.Alive;
[SerializeField]
float rcsThrust = 100f;
[SerializeField]
float mainThrust = 100f;
[SerializeField]
AudioClip mainEngine;
[SerializeField]
AudioClip death;
[SerializeField]
AudioClip nextLevel;
AudioSource audioSource;
Rigidbody rigidBody;
// Start is called before the first frame update
void Start()
{
audioSource = GetComponent();
rigidBody = GetComponent();
}

// Update is called once per frame
void Update()
//switch off audio after dying
{
    if(state == State.Alive){
        Thrust();
        Rotate();
    }
    }

void OnCollisionEnter(Collision collision)
{
    if (state != State.Alive) { return; }
    switch (collision.gameObject.tag)
    {
        
        case "Friendly":
            //do nothing
            break;
        case "Finish":
            StartSuccesSequence();
            break;
        default:
            StartDeathSequence();
            break;
    }
}

private void StartDeathSequence()
{
    state = State.Dying;
    audioSource.Stop();
    DyingSound();
    Invoke("LoadFirstScene", 3f);
}

private void StartSuccesSequence()
{
    state = State.Transcending;
    audioSource.Stop();
    NextLevelSound();
    Invoke("LoadNextScene", 3f);
}

private void DyingSound()
{
    audioSource.PlayOneShot(death);
}

private void LoadFirstScene()
{
    SceneManager.LoadScene(0);
}

private void LoadNextScene()
{
    SceneManager.LoadScene(1);
}

private void NextLevelSound()
{
    audioSource.PlayOneShot(nextLevel);
}

private void Thrust()
{
    if (Input.GetKey(KeyCode.Space)) //can thrust while rotating
    {
        ApplyThrust();
    }
    else
    {
        audioSource.Stop();
    }
}

private void ApplyThrust()
{
    if (!audioSource.isPlaying)
    {
        audioSource.PlayOneShot(mainEngine);
    }
    rigidBody.AddRelativeForce(Vector3.up * mainThrust);
}

private void Rotate()
{

    float rotationThisFrame = rcsThrust * Time.deltaTime;
    rigidBody.freezeRotation = true; // take manual control of rotation
    if (Input.GetKey(KeyCode.A))
    {
        transform.Rotate(Vector3.forward * rotationThisFrame);
    }
    else if (Input.GetKey(KeyCode.D))
    {
        transform.Rotate(-Vector3.forward * rotationThisFrame);
    }
    rigidBody.freezeRotation = false; //resume physics control of rotation

}

}

Privacy & Terms