It’s me again with clipping sound
Here is my current code. I bet you are experiencing clipping sound again (I experience it with death sound ). I could not find a decent solution for that. The only thing that helped was to increase the invoke time to approx. length of the death track.
Here is my current code:
using UnityEngine;
using UnityEngine.SceneManagement;
public class Rocket : MonoBehaviour {
[SerializeField] float rcsThrust = 100f;
[SerializeField] float mainThrust = 10f;
[SerializeField] AudioClip mainEngine;
[SerializeField] AudioClip deathSound;
[SerializeField] AudioClip levelLoadSound;
Rigidbody rigidBody;
AudioSource audioSource;
enum State { Alive, Dying, Transcending };
State state = State.Alive;
// Use this for initialization
void Start () {
rigidBody = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update () {
//todo stop sound clipping on death
if (state == State.Alive)
{
RespondToThrustInput();
RespondToRotateInput();
}
}
// Call whenever there is a collision
private void OnCollisionEnter(Collision collision)
{
if (state != State.Alive) { return; } // ignore collisions on death
switch (collision.gameObject.tag)
{
case "Friendly":
// do nothing
break;
case "Finish":
StartSuccessSequence();
break;
default:
StartDeathSequence();
break;
}
}
private void StartSuccessSequence()
{
state = State.Transcending;
audioSource.Stop();
audioSource.clip = levelLoadSound;
audioSource.volume = 0.3f;
audioSource.Play();
Invoke("LoadNextLevel", 1f); //todo parameterize parameter
}
private void StartDeathSequence()
{
state = State.Dying;
audioSource.Stop();
audioSource.clip = deathSound;
audioSource.volume = 0.3f;
audioSource.Play();
Invoke("LoadFirstLevel", 2.7f); //todo parameterize parameter
}
private void LoadFirstLevel()
{
SceneManager.LoadScene(0);
}
private void LoadNextLevel()
{
SceneManager.LoadScene(1); // todo allow for more levels
}
private void RespondToThrustInput()
{
if (Input.GetKey(KeyCode.Space))
{
ApplyThrust();
}
else
{
// Stop audio source gentle way
FadeOut();
}
}
private void ApplyThrust()
{
rigidBody.AddRelativeForce(Vector3.up * mainThrust);
audioSource.clip = mainEngine;
if (!audioSource.isPlaying)
{
audioSource.Play();
}
audioSource.volume = 0.3f;
}
private void FadeOut()
{
if (audioSource.volume > 0)
{
audioSource.volume -= 0.3f * Time.deltaTime;
}
}
private void RespondToRotateInput()
{
rigidBody.freezeRotation = true;
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;
}
}
The problem is that you reinstantiate your audio on scene load so you can’t just fade out the death sound. Possible solution could be here:
https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
but it is to much work in this stage of project
Btw. I used audioSource.clip instead of audioSource.PlayOneShot() because you just cannot stop the track.
Have you a solution for the death clipping sound ? Feel free to join the discussion !