Audio bug easy fix

If you keep movement script enabled when the success collision happens instead of disabling it like we did for the crash collision, I believe the movement script is still going to fall into the else statement that is telling the audio source to stop playing on every frame update. So, while technically the “success” audioclip is still being played, it is immediately being stopped on the next frame update. This can be solved by introducing some public bools that will help define the state of the game object. I declared two public bools and used one of them in the an “if” statement and the other in the “else if” statement of my “Movement” script. Then referenced the “Movement” script within the “CollisionHandler” script in order to change state of the public bools during their associated events.

Here is my movement script for example:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
[SerializeField] float mainThrust = 1f;
[SerializeField] float turnTorque = 1f;
[SerializeField] AudioClip mainEngine;

Rigidbody rocketRigidbody;
AudioSource audioSource;

public bool isAlive = true;
public bool hasFinished = false;

private void Awake() 
{
    rocketRigidbody =  GetComponent<Rigidbody>(); 
    audioSource = GetComponent<AudioSource>();   
}
void Start()
{

}

void Update()
{
    ProcessThrust();
    ProcessRoatation();
}
void ProcessThrust()
{
    if(Input.GetKey(KeyCode.Space) && isAlive)
    {
        rocketRigidbody.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);
        if(!audioSource.isPlaying)
        {
            audioSource.PlayOneShot(mainEngine);

        }
    }
    else if (audioSource.isPlaying && !hasFinished)
        {
            audioSource.Stop();
        }
}
void ProcessRoatation()
{
    if(Input.GetKey(KeyCode.A))
    {
        ApplyRotation(turnTorque);
    }
    if (Input.GetKey(KeyCode.D))
    {
        ApplyRotation(-turnTorque);
    }

}

private void ApplyRotation(float rotationThisFrame)
{
    rocketRigidbody.AddRelativeTorque(Vector3.forward * rotationThisFrame * Time.deltaTime);
}

}

Then here is my CollisionHandler script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class CollisionHandler : MonoBehaviour
{

[SerializeField] float timeToLoadNextScene = 3f;
[SerializeField] float timeToReloadScene = 1f;
[SerializeField] AudioClip successSFX;
[SerializeField] AudioClip crashSFX;

AudioSource audioSource; 
Movement movement;

private void Awake() 
{
    audioSource = GetComponent<AudioSource>();  
    movement = GetComponent<Movement>();  
}

private void OnCollisionEnter(Collision other) 
{
    switch(other.gameObject.tag)
    {
        case "Friendly":
            Debug.Log("This is Friendly");
            break;
        case "Finish":
            Debug.Log("Finish!");
            SuccessSequence();
            audioSource.PlayOneShot(successSFX);
            break;
        case "Respawn":
            Debug.Log(SceneManager.GetActiveScene().buildIndex);
            break;
        default:
            Debug.Log("BOOM");
            CrashSequence();
            break;
    }    
}
void SuccessSequence()
{
    movement.hasFinished = true;    
    audioSource.PlayOneShot(successSFX);
    Invoke("LoadNextScene" , timeToLoadNextScene);
}
void CrashSequence()
{
    movement.isAlive = false;
    audioSource.PlayOneShot(crashSFX);
    GetComponent<Movement>().enabled = false;
    Invoke("ReloadScene" , timeToReloadScene);
}

void ReloadScene()
{
    int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
    SceneManager.LoadScene(currentSceneIndex);
}
void LoadNextScene()
{
    int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
    int sceneCount = SceneManager.sceneCountInBuildSettings;
    int nextSceneIndex = currentSceneIndex+1;
    if(nextSceneIndex == sceneCount)
    {
        Restart();
    }
    else
    {
        SceneManager.LoadScene(nextSceneIndex);

    }
}
void Restart()
{
    SceneManager.LoadScene(0);
}

}

2 Likes

This is happening as we have not disbled controls on success as we have done on crash.
Thanks for point, before this I was not able to spot the problem.

Privacy & Terms