Problem with particle effects and detonating bombs
I want to add a particle effect to the detonate function. I would like the bomb to explode with the sound and particle effect at 15s and when the player enters the trigger box for 3s. The coroutine only works under the ontriggerenter function and not under the if statement nor in the detonate function. Please help I don't know what I am doing wrong.
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class Explosion : MonoBehaviour {
public GameObject bomb;
public float power = 10.0f;
public float radius = 5.0f;
public float upForce = 1.0f;
public AudioSource bombExplosion;
public AudioSource tickingBomb;
float damage = 5f;
public GameObject explosionPlease;
void Start()
{
Invoke("Detonate", 15);
bombExplosion.PlayDelayed(14);
Debug.Log("Explosion music");
tickingBomb.PlayDelayed(12);
explosionPlease.SetActive(false);
}
void Detonate()
{
Vector3 explosionPosition = bomb.transform.position;
Collider[] colliders = Physics.OverlapSphere (explosionPosition, radius);
foreach (Collider hit in colliders)
{
Rigidbody rb = hit.GetComponent<Rigidbody> ();
if (rb != null)
{
rb.AddExplosionForce (power, explosionPosition, radius, upForce, ForceMode.Impulse);
}
if (hit.CompareTag ("Player"))
{
hit.gameObject.GetComponent<MainCharacterHealth> ().TakeDamage (damage);
}
}
var myHealth = GetComponent<BomberEnemyHealth>();
myHealth.TakeDamage(myHealth.max_Health);
Destroy (gameObject);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
StartCoroutine(DelayParticleEffect(3));
tickingBomb.Play();
Debug.Log("Ticking sound");
bombExplosion.PlayDelayed(2);
Debug.Log("Explosion music");
Invoke("Detonate", 3);
Debug.Log("Quick denotate");
}
}
private IEnumerator DelayParticleEffect(float duration)
{
explosionPlease.SetActive(false);
yield return new WaitForSeconds(3);
explosionPlease.SetActive(true);
}
}
Your answer
Follow this Question
Related Questions
IEnumerator skips remaining function after yield 1 Answer
Can Unity throw an error when I start a coroutine without StartCoroutine? 0 Answers
How do you use emission.rate? 2 Answers
ParticleSystem.Bake() not working with specific particle 0 Answers
Particles disappear when the parent rotates,My particles disappear when the parent rotates 0 Answers