I am having Issue regarding finishing Level clip loading.Both thrust and Death audioclip works perfectly well but once it reaches the landing Pad, it waits for the seconds specified and transcend to next scene but fails to play the audioClip.
Here is a video demo of the game play
I’m not sure where the bug is from but. I’m sure the
StartFinishSequence()
function loads Next Level without issue.
I assigned necessary audio as well in the inspector
Below is my Rocket.cs code
using UnityEngine;
using UnityEngine.SceneManagement;
public class Rocket : MonoBehaviour
{
// Rigid Comp
private Rigidbody m_rigidbody;
AudioSource audioSource;
//Variables
[SerializeField] float mainThrust = 100f;
[SerializeField] float rcsThrust = 100f;
//AudioClips
[SerializeField] AudioClip mainThrustClip;
[SerializeField] AudioClip finishLevelClip;
[SerializeField] AudioClip deathExplosionClip;
enum State { Alive, Dead, Transcending};
[SerializeField] State state = State.Alive;
// Start is called before the first frame update
void Start()
{
m_rigidbody = GetComponent<Rigidbody>(); // get the Rigid Comp for ship
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
// todo stop sound on death
if (state != State.Dead)
{
Thrust();
Rotate();
}
}
private void OnCollisionEnter(Collision collision)
{
if (state != State.Alive) return; // Ignore collision when dead or transcending
string tag = collision.gameObject.tag;
switch (tag)
{
case "Friendly" :
// do nothing
break;
case "Enemy":
StartDeathSequence();
break;
case "Finish":
StartFinishSequence();
break;
default:
Debug.Log("I'm Not tagged");
break;
}
}
private void StartFinishSequence()
{
state = State.Transcending;
audioSource.Stop(); // stop any other audio
audioSource.PlayOneShot(finishLevelClip);
Invoke("LoadNextScene", 2f);
}
private void StartDeathSequence()
{
state = State.Dead;
audioSource.Stop(); // stop any other audio
audioSource.PlayOneShot(deathExplosionClip);
Invoke("LoadBeginningScene", 2f);
}
private void LoadBeginningScene()
{
SceneManager.LoadScene(0);
}
private void LoadNextScene()
{
SceneManager.LoadScene(1);
}
private void Thrust()
{
if (Input.GetKey(KeyCode.Space))
{
// Applying thrust
m_rigidbody.AddRelativeForce(Vector3.up * Time.deltaTime * mainThrust * 10);
if (!audioSource.isPlaying )
audioSource.PlayOneShot(mainThrustClip);
}
else
{
audioSource.Stop();
}
}
private void Rotate()
{
// pause RB rotation force while performing our own
m_rigidbody.freezeRotation = true;
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
transform.Rotate(Vector3.forward * Time.deltaTime * rcsThrust);
}
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
transform.Rotate(-Vector3.forward * Time.deltaTime * rcsThrust);
}
// Resumes RB rotation force
m_rigidbody.freezeRotation = false;
}
}
I wil appreciate if any help can be rendered