Still doesnt work :( PLS HELP ITS DRIVING ME INSANE [Fixed]

Continuing the discussion from Audio Source "Success" sound not playing:
[Update: Whats weird is that the sound effect only plays when the top half of the rocket hits the landing pad]
I disabled the Movement script and the success sound doesnt work.
(I cached the Movement class)

Collision Handler:

using UnityEngine;
using UnityEngine.SceneManagement;

public class CollisionHandler : MonoBehaviour
{
    [SerializeField] float crashDelay = 1f;
    [SerializeField] float loadDelay = 1f;
    [SerializeField] AudioClip crashSFX;
    [SerializeField] AudioClip successSFX;

    Movement movementScript;
    AudioSource audioSource;

    void Start()
    {
        audioSource = GetComponent<AudioSource>();
        movementScript = GetComponent<Movement>();
    }
    
    void OnCollisionEnter(Collision other)
    {
        switch (other.gameObject.tag)
        {
            case "Friendly":
                Debug.Log("This is Friendly");
                break;
            case "Finish":
                StartSuccessSequence();
                break;
            default:
                StartCrashSequence();
                break;
        }
    }

    void StartSuccessSequence()
    {
        audioSource.PlayOneShot(successSFX);
        movementScript.enabled = false;
        Invoke("LoadNextLevel", loadDelay);
    }

    void StartCrashSequence()
    {
        audioSource.PlayOneShot(crashSFX);
        movementScript.enabled = false;
        Invoke("ReloadLevel", crashDelay);
    }
    
    void ReloadLevel()
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene(currentSceneIndex);
    }
    
    void LoadNextLevel()
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        int nextSceneIndex = currentSceneIndex + 1;
        if(nextSceneIndex == SceneManager.sceneCountInBuildSettings)
        {
            nextSceneIndex = 0;
        }
        SceneManager.LoadScene(nextSceneIndex);
    }
}

Movement:

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

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

    Rigidbody rb;
    AudioSource audioSource;

    void Start()
    {
       rb = GetComponent<Rigidbody>();
       audioSource = GetComponent<AudioSource>();
    }

    void Update()
    {
       ProcessThrust(); 
       ProcessRotation();
    }

    void ProcessThrust()
    {
        if(Input.GetKey(KeyCode.Space))
        {
            rb.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);

            if(!audioSource.isPlaying)
            {
                audioSource.PlayOneShot(mainEngine);  
            }
           
        }
        else
        {
            audioSource.Stop(); 
        }        
    }

    void ProcessRotation()
    {
        if(Input.GetKey(KeyCode.A))
        {
	        ApplyRotation(rotationThrust);
        }
        else if(Input.GetKey(KeyCode.D))
        {
            ApplyRotation(-rotationThrust);
        }
    }

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

1 Like

Welcome to the community!

That’s kinda weird… That should solve the issue unless there’s a very, very obscure racing going on there, it might be the case depending on how slow (or fast) the audio source’s stop and play function are, and since both methods are running in a different clock it might be the issue, but, no, I tdon’t hink that’s the case because it doesn’t explain the half of the rocket not triggering the sound.

Are you using multiple colliders for your rocket? If that’s the case be sure that none of them are set as triggers, that would prevent the OnCollisionEnter function from getting called, but other than that I cannot think of anything else that might cause what you are describing.

I think Rick solves the issue later in the course, you might also want to ignore this until it gets adressed by him.

1 Like

Yeah i fixed it, by using bool variables

1 Like

If it isn’t much trouble, Could you share the code so other students can see how you fixed the issue?

The fix is

bool isTransitioning = false;

And:

 if(isTransitioning)
        {
            return;
        }
        switch (other.gameObject.tag)
        {
            case "Friendly":
                Debug.Log("This is Friendly");
                break;
            case "Finish":
                StartSuccessSequence();
                break;
            default:
                StartCrashSequence();
                break;
        }
using UnityEngine;
using UnityEngine.SceneManagement;

public class CollisionHandler : MonoBehaviour
{
    [SerializeField] float loadDelay = 1f;
    [SerializeField] AudioClip crashSFX;
    [SerializeField] AudioClip successSFX;
    
    [SerializeField] ParticleSystem crashParticles;
    [SerializeField] ParticleSystem successParticles;


    Movement movementScript;
    AudioSource audioSource;

    bool isTransitioning = false;

    void Start()
    {
        audioSource = GetComponent<AudioSource>();
        movementScript = GetComponent<Movement>();       
    }
    
    void OnCollisionEnter(Collision other)
    {
        if(isTransitioning)
        {
            return;
        }
        switch (other.gameObject.tag)
        {
            case "Friendly":
                Debug.Log("This is Friendly");
                break;
            case "Finish":
                StartSuccessSequence();
                break;
            default:
                StartCrashSequence();
                break;
        }
    }

    void StartSuccessSequence()
    {
        isTransitioning = true;
        audioSource.Stop();
        audioSource.PlayOneShot(successSFX);
        successParticles.Play();
        movementScript.enabled = false;
        Invoke("LoadNextLevel", loadDelay);
    }

    void StartCrashSequence()
    {
        isTransitioning = true;
        audioSource.Stop();
        audioSource.PlayOneShot(crashSFX);
        crashParticles.Play();
        movementScript.enabled = false;
        Invoke("ReloadLevel", 1f);
    }
    
    void ReloadLevel()
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene(currentSceneIndex);
    }
    
    void LoadNextLevel()
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        int nextSceneIndex = currentSceneIndex + 1;
        if(nextSceneIndex == SceneManager.sceneCountInBuildSettings)
        {
            nextSceneIndex = 0;
        }
        SceneManager.LoadScene(nextSceneIndex);
    }
}

1 Like

Privacy & Terms