Everything works but audio

At a genuine loss. Debugs work, audio work on thrust only, which is apart of the Movement script

using UnityEngine;
using UnityEngine.SceneManagement;

public class CollisionBehavior : MonoBehaviour
{

    [SerializeField] float holdLevel;
    [SerializeField] AudioClip youWin;
    [SerializeField] AudioClip youLose;


    AudioSource audioSource;


    private void Start()
    {
        audioSource = GetComponent<AudioSource>();
    }
    void OnCollisionEnter(Collision other)
    {
       

        switch (other.gameObject.tag)
        {
            case "Friendly":
                Debug.Log("Friendly");
                break;
            case "Fuel":
                Debug.Log("More Fuel");
                break;
            case "Finish":
                StartSuccessSequence();
                break;
            default:
                StartCrashSequence();
                break;

        }

    }

        //When Player Wins
        void StartSuccessSequence()
        {
            Debug.Log("But have you won?");
            Invoke("NextLevel", holdLevel);
            audioSource.PlayOneShot(youWin);

        }

        //When Player Crashs
        void StartCrashSequence()
        {
            Debug.Log("You've Crashed");
            audioSource.PlayOneShot(youLose);
            Invoke("ReloadLevel", 10f);

        }
        // If crashed reloads scene from Unity Build through index numbers
        void ReloadLevel()
        {
            int currentScene = SceneManager.GetActiveScene().buildIndex;
            SceneManager.LoadScene(currentScene);
        }

        // If succeed loads scene scene using Unity Build index number. Adds 1 from current scene. 
        void NextLevel()
        {
            int currentScene = SceneManager.GetActiveScene().buildIndex;
            SceneManager.LoadScene(currentScene + 1);

        }
     
}

Here is also the Movement Script.

using UnityEngine;

public class Movement : MonoBehaviour
{



    [SerializeField] float mainThrust = 100;
    [SerializeField] float mainRotation = 100;
    [SerializeField] AudioClip mainEngine; 

    Rigidbody rb;
    AudioSource audioSource;
  
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        audioSource = rb.GetComponent<AudioSource>();
   
    }
  

    // Method ProcessInput THRUST
    void ProcessInput()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            Debug.Log(" You have pressed Thurster ");
            rb.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);
            if (!audioSource.isPlaying)
            {

                audioSource.PlayOneShot(mainEngine);
            }
        }

        else
        {
            audioSource.Stop();
        }
        
    }


    // Method ProcessRotation ROTATION
    void ProcessRotation()
    { 
            
            if (Input.GetKey(KeyCode.D))
        {
            ApplyRotation(mainRotation);
        }

            else if (Input.GetKey(KeyCode.A))
        {
            ApplyRotation(-mainRotation);
        }
    }

   void ApplyRotation( float mainRotation)
    {
        rb.freezeRotation = true;
        transform.Rotate(Vector3.back * mainRotation * Time.deltaTime);
        rb.freezeRotation = false;
    }


    //Updates everyframe
    void Update()
    {
        ProcessInput();
        ProcessRotation();
        rb.mass = .05f;

    }
        
   


}



.

I can’t see anything wrong (except StartSuccessSequence(), see below), but I can also not see all the code. Can you paste all the code from CollisionBehaviour.cs? Not a screenshot, see here: How to apply code formatting within your post

With the current configuration your ‘win’ audio will not play. Your Invoke delay is set to 0 and will load the scene immediately, which will destroy the current AudioSource.

I believe the culprit is in the Movement script, look at this part:

 // Method ProcessInput THRUST
    void ProcessInput()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            Debug.Log(" You have pressed Thurster ");
            rb.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);
            if (!audioSource.isPlaying)
            {

                audioSource.PlayOneShot(mainEngine);
            }
        }

        else
        {
            audioSource.Stop();
        }
        
    }

This means that both Movement and CollisionHandler are taking over the AudioSource component, and the Movement script is telling the AudioSource to Stop any time the Space key is not pressed, which is not what we want.

I’ve fixed it for me so that I’ve turned off the Movement script in both the case we crash or reach the Landing Pad - this disables the Movement script stopping the audio, so the Clip can play.

I did it like this:

   private void StartSuccessSequence() {
        audioSource.PlayOneShot(successSFX);
        GetComponent<Movement>().enabled = false;        
        Invoke(LOAD_NEXT_LEVEL, loadDelay);
    }

    private void StartCrashSequence() {
        audioSource.PlayOneShot(crashSFX, .1f);
        // TODO add Particle System
        GetComponent<Movement>().enabled = false;        
        Invoke(RELOAD_LEVEL, loadDelay);
    }
2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms