Unsure as to why but the explosion and success particles are playing on top of each other on every play. This is true outside of code, so I don’t believe it is code related.
I dragged both particles as children of my rocket and in the inspector, dragged in the particles from the hierarchy and not the prefabs. Where am I going wrong here?
public class CollisionHandler : MonoBehaviour
{
[SerializeField] float delay;
[SerializeField] AudioClip crashSound;
[SerializeField] AudioClip winSound;
[SerializeField] ParticleSystem explosion;
[SerializeField] ParticleSystem success;
Movement movement;
AudioSource audioSource;
bool playAudio;
private void Start()
{
audioSource = GetComponent<AudioSource>();
explosion = GetComponent<ParticleSystem>();
success = GetComponent<ParticleSystem>();
playAudio = true;
}
void OnCollisionEnter(Collision collision)
{
delay = 2f;
switch(collision.gameObject.tag)
{
case "Friendly":
Debug.Log("Friendly");
break;
case "Fuel":
Debug.Log("Fuel");
break;
case "Finish":
Debug.Log("You win");
StartCoroutine(StartNextLevelSequence());
break;
default:
Debug.Log("You blew up");
StartCoroutine(StartCrashSequence());
break;
}
void ReloadLevel()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(currentSceneIndex);
}
void LoadNextLevel()
{
int nextSceneIndex = SceneManager.GetActiveScene().buildIndex + 1;
if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
{
nextSceneIndex = 0;
}
SceneManager.LoadScene(nextSceneIndex);
}
IEnumerator StartCrashSequence()
{
explosion.Play();
PlayAudio(crashSound);
movement = GetComponent<Movement>();
movement.enabled = false;
yield return new WaitForSeconds(delay);
ReloadLevel();
}
IEnumerator StartNextLevelSequence()
{
//plays the Win Sound and Success Particles
success.Play();
PlayAudio(winSound);
//turns off player controls on impact
movement = GetComponent<Movement>();
movement.enabled = false;
//waits X seconds then reloads level
yield return new WaitForSeconds(delay);
LoadNextLevel();
}
void PlayAudio(AudioClip audio)
{
if (playAudio) //evaluates boolean of playAudio (default true) then plays the audio and disables play audio to prevent stacking
{
audioSource.Stop();
audioSource.PlayOneShot(audio);
playAudio = false;
}
}
}
}