I’m having an issue where my audio is not playing for the death or success states but it is having no issues with the thrust noise.
I diffed my cs and the project cs to see what was different to try and figure it out and checked a few other peoples issues out but I couldn’t figure it out.
The only major difference in my script is how I am disabling controls but I don’t think that would affect the audio.
I did try removing the audiosource.Stop(); in the success and death sequence methods but that did nothing.
Any tips or suggestions to try out are most welcome!
I am running on unity 2020.1.16f1.
Thanks
using UnityEngine;
using UnityEditor.SceneManagement;
public class Rocket : MonoBehaviour
{
Rigidbody rigidbody;
AudioSource audioSource;
[SerializeField] float rcsThrust = 100f;
[SerializeField] float mainThrust = 25f;
[SerializeField] AudioClip mainEngine;
[SerializeField] AudioClip deathSound;
[SerializeField] AudioClip loadingSound;
bool enableControls = true;
enum State { Alive, Dying, Trancending }
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()
{
ProcessInput();
}
private void ProcessInput()
{
Thrust();
Rotate();
}
void OnCollisionEnter(Collision collision)
{
switch (collision.gameObject.tag)
{
case "Friendly":
break;
case "Finish":
SuccessSequence();
break;
default:
DeathSequence();
break;
}
}
private void DeathSequence()
{
state = State.Dying;
enableControls = false;
audioSource.Stop();
audioSource.PlayOneShot(deathSound);
Invoke("DeathScene", 2f);
}
private void SuccessSequence()
{
state = State.Trancending;
enableControls = false;
audioSource.Stop();
audioSource.PlayOneShot(loadingSound);
Invoke("LoadNextScene", 2f);
}
private void LoadNextScene()
{
EditorSceneManager.LoadScene(1); //Allow for increment levels
}
private void DeathScene()
{
EditorSceneManager.LoadScene(0);
}
private void Rotate()
{
rigidbody.freezeRotation = true; //Take manual contorl of rotation
float rotationThisFrame = rcsThrust * Time.deltaTime;
if (Input.GetKey(KeyCode.A) && enableControls == true)
{
transform.Rotate(Vector3.forward * rotationThisFrame);
print("A key pressed");
}
else if (Input.GetKey(KeyCode.D) && enableControls == true)
{
transform.Rotate(-Vector3.forward * rotationThisFrame);
print("D key pressed");
}
rigidbody.freezeRotation = false; //Resume physics roation
}
private void Thrust()
{
if (Input.GetKey(KeyCode.Space) && enableControls == true ) //can thrust while rotating
{
ResponToThurst();
print("space key pressed");
}
else
{
audioSource.Stop();
}
}
private void ResponToThurst()
{
rigidbody.AddRelativeForce(Vector3.up * mainThrust);
if (!audioSource.isPlaying)
{
audioSource.PlayOneShot(mainEngine);
}
}
}