Tell me how to activate explosion particle on and sound for 5 second when I throw grenade. Please tell me.
Thanks in Advance !
Just add start delay (in second) in the particle system main module. As for the sound, you need to add another game object with AudioSource component, and then write a script to delay the activation of the gameobject or its audio component in coroutine.
check collision if it’s ground then start coroutine or use time like this
if (// grounded)
{
Lifetime -=Time.deltaTime
}
put lifetime =5
From what i understand, you have a grenade that has a particle system attached to it. You want the particle system and the audio to start after 5 seconds from when it hits the ground. A good way to do this is through collision detection with the ground. Once it collided, you will start a Coroutine()
and tell it to play the audio and the particle system after 5 seconds. This is what you will need:
- A grenade with a Rigidbody to give it the natural physics. And a collider to detect collision with the ground. (The ground must have a collider as well).
- A particle system that is a child of the genade (the parent). And an Audio Source element and add the explosion sound to it.
- Create a tag for the ground with the name “Ground”
- Create a script and attach it to the grenade.
This is how the script should look like:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GrenadeScript : MonoBehaviour {
public ParticleSystem explosionParticle;
private AudioSource explosionSound;
private int explosionEnabler = 0;
private void Start()
{
explosionSound = gameObject.GetComponent<AudioSource>();
}
private void OnCollisionEnter(Collision coll)
{
if (explosionEnabler == 0)
{
if (coll.gameObject.CompareTag("Ground"))
{
explosionEnabler = 1;
StartCoroutine(Explosion());
}
}
}
IEnumerator Explosion()
{
yield return new WaitForSeconds(5f);
explosionParticle.gameObject.SetActive(true);
explosionSound.Play();
}
}
This should do the trick.
I attached an image so you can follow the setup exactly
All the best in making you game