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;
}
}