The rocket engine sound will keep playing when I finish the first level, though I keep my hand off the space bar. The code below is what I have for the rocket. Only the segment in the method processThrust() is related to audio at this stage. Is there a way to stop audio when first level is finished?
using UnityEngine;
public class Movement : MonoBehaviour
{
// Start is called before the first frame update
Rigidbody rigid;
AudioSource audio;
[SerializeField]float mainThrust=400f;
[SerializeField]float rotationThrust=40f;
void Start()
{
rigid=GetComponent<Rigidbody>();
audio=GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
processThrust();
processRotation();
}
void processThrust(){
Debug.Log(Vector3.up * mainThrust*Time.fixedDeltaTime);
if(Input.GetKey(KeyCode.Space)){
rigid.AddRelativeForce(Vector3.up * mainThrust*Time.fixedDeltaTime);
if(!audio.isPlaying){
audio.Play();
}
}else{
audio.Stop();
}
}
void processRotation(){
if(Input.GetKey(KeyCode.A))
{
applyRotation(rotationThrust);
}
else if(Input.GetKey(KeyCode.D)){
applyRotation(-rotationThrust);
}
}
private void applyRotation(float rotationFrame)
{
rigid.freezeRotation=true;
transform.Rotate(rotationFrame * Vector3.forward * Time.fixedDeltaTime);
rigid.freezeRotation=false;
}
}