How to make Particle System on when I throw grenades on ground after 5 second.
Tell me how to activate explosion particle on and sound for 5 second when I throw grenade. Please tell me. Thanks in Advance !
so you want the grenade to explode when it touches the ground? or after its thrown and left the hand of the player? or once the player takes the grenade out (in this case it will explode in five seconds and that could also kill the player)?
no when the grenade hit the ground then a audio will play and after 5 seconds it will explode with particle system and with a explosion audio. Give me script please if you know.
Do you have a code that i can take a look at? or should i just write a script from what i understand?
Answer by tmalhassan · Sep 23, 2017 at 02:00 PM
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 :)
But how my player throw grenade ? my player need force to throw grenade, I have already tell you that I don`t have any script. And yes it throw grenade only when I press F after pressing F then my player automatically take his gun and start fiiring. Please do something more for me. This last question please, Please! I have to accept your correct answer.
Tell me how to activate explosion particle on and sound for 5 second when I throw grenade. Please tell me. Thanks in Advance !
Your question doesn't mention anything about instantiating the grenade. It only talks about playing the particle system and the audio after 5 seconds from hitting the ground. So as far as i'm concerned, this question is answered. You can post another question and explain what you want in details.
Answer by ifurkend · Sep 21, 2017 at 04:48 AM
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.
Would you give me codes because I noob to unity3d please. I know you have lot of time please. Using coroutine is very diffcult for me. Please plzzzzzzzzz
If you are a one-man band and want to make your own game with Unity, learning C# scripting is inescapable, you should begin watching Unity scripting tutorial right now. One thing I did mistake is that AudioSource.Play or PlayScheduled provide properties for delay, removing the trouble to write a coroutine, even though I suspect it's a wrapper of coroutine.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class audioDelay : $$anonymous$$onoBehaviour
{
public double delay = 5.0;
void OnEnable()
{
AudioSource audio = GetComponent<AudioSource>();
audio.PlayScheduled(delay);
}
}
Bro give me particle script when I throw grenade then after 5 second the the particles become activated. Sorry ! your answers in not accepted if you give me this script I definitely accept your answer.
Answer by kskjadav007 · Sep 21, 2017 at 05:17 AM
check collision if it's ground then start coroutine or use time like this
if (// grounded) { Lifetime -=Time.deltaTime }
put lifetime =5