It is the first time I am encountering this issue. I am making a script that changes the effect of wind on a Player after X amount of time and which lasts for Y amount of time. I need my sound to play together with this effect, however, it always plays only AFTER the event finishes. Here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SnowScript : MonoBehaviour
{
public static SnowScript instance;
AudioManager audioManager;
IndentScript indentScript;
Vector2 originalPos = new Vector2(-6.51f,7.53f);
Quaternion originalRot = Quaternion.Euler(110,-90,-90);
ParticleSystem ps;
public float delay;
[Range(0f, 2f)]
public float randomDelay;
public bool swapped;
public float snowAffect;
public float timer;
public float burstDelay;
[Range(0f, 2f)]
public float randomBurstDelay;
public float snowBurstAffect;
public bool burst;
public float burstTimer;
public float burstActiveTime;
void Awake()
{
instance = this;
timer = delay;
burstTimer = burstDelay;
transform.position = originalPos;
transform.rotation = originalRot;
ps = GetComponent<ParticleSystem>();
}
void Start()
{
audioManager = AudioManager.instance;
indentScript = IndentScript.instance;
}
void Update()
{
CheckTimers();
CheckSwap();
}
void CheckTimers()
{
if (timer >= 0)
{
timer -= Time.deltaTime;
}
if (timer <= 0)
{
swapped = !swapped;
Swap();
}
if (burstTimer > 0)
{
burstTimer -= Time.deltaTime;
}
if (burstTimer <= 0)
{
burst = true;
}
}
void CheckSwap()
{
if (swapped)
{
if (!burst)
{
var main = ps.main;
main.simulationSpeed = 1f;
indentScript.transform.Translate(Vector3.left * snowAffect * Time.deltaTime);
}
if (burst)
{
var main = ps.main;
main.simulationSpeed = 3f;
StartCoroutine("Wait");
indentScript.transform.Translate(Vector3.left * snowBurstAffect * Time.deltaTime);
}
}
if (!swapped)
{
if (!burst)
{
var main = ps.main;
main.simulationSpeed = 1f;
indentScript.transform.Translate(Vector3.right * snowAffect * Time.deltaTime);
}
if (burst)
{
var main = ps.main;
main.simulationSpeed = 3f;
StartCoroutine("Wait");
indentScript.transform.Translate(Vector3.right * snowBurstAffect * Time.deltaTime);
}
}
}
void Swap()
{
if (!swapped)
{
transform.position = originalPos;
transform.rotation = originalRot;
}
if (swapped)
{
transform.position = new Vector2(10.91f, 7.53f);
transform.rotation = Quaternion.Euler(20, -90, -90);
}
timer = delay * (1 + Random.Range(-randomDelay / 2f, randomDelay / 2f));
}
IEnumerator Wait()
{
//audioManager.PlaySound("WindBurst"); // - Sound should play BEFORE the WaitForSeconds, however it only plays after.
yield return new WaitForSeconds(burstActiveTime);
burst = false;
burstTimer = burstDelay * (1 + Random.Range(-randomBurstDelay / 2f, randomBurstDelay / 2f));
}
}