how to activate a particle when bullet hits a collider?
so, im trying to make a explosion particle appear once when the bullet hits any collider. i used instantiate to make the explosion particle appear when its called but the particles do not appear, instead the instance of it is just created without the particles being animated.
tried to put (ParticleSystem.enableEmission = True) somewhere but the code would always come up with some syntax error or something... im still a beginner in programming. So how can i put that in the code? or, is there a better way to achieve the result i want? thanks in advance!
my script used on the bullet prefab:
public class blueLaserScript : MonoBehaviour
{
public ParticleSystem explosionParticle;
public int dmg = 1;
public float lifeTime = 3f;
public float maxLifeTime = 0.35f;
void Start()
{
lifeTime = maxLifeTime;
}
void FixedUpdate()
{
if (lifeTime > 0)
{
lifeTime -= Time.deltaTime;
}
else
{
Destroy(gameObject);
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.isTrigger == !true && !col.CompareTag("Enemy"))
{
Instantiate(explosionParticle, transform.position, transform.rotation);
Destroy(gameObject);
}
if (col.isTrigger == !true && col.CompareTag("Enemy"))
{
col.SendMessageUpwards("Damage", dmg);
Instantiate(explosionParticle, transform.position, transform.rotation);
Destroy(gameObject);
}
}
}
Answer by UCSIM · Feb 22, 2016 at 10:58 PM
If I am understanding correctly you are trying to get the particle system to start emitting. The method I use for this is calling Play() on a particle system object. Then you can set .enable to false when you're done with it.
Hopefully I answered your question.
thanks it helped! took me a while to figure on how to put it though. made a separate script for the particle:
public float lifeTime = 0.5f;
public float maxLifeTime = 0.26f;
private ParticleSystem thisExplosion;
void Start ()
{
lifeTime = maxLifeTime;
thisExplosion = gameObject.GetComponent<ParticleSystem>();
}
void Update()
{
thisExplosion.Play();
if (lifeTime > 0)
{
lifeTime -= Time.deltaTime;
}
else
{
Destroy(gameObject);
}
}
Your answer
Follow this Question
Related Questions
How to move a bullettrail the direction the player is facing 0 Answers
How to create a link between particle and script C# 1 Answer
Add a public Tranform target from another scene 0 Answers
NullReferenceException: Object reference not set to an instance of an object, again 2 Answers
How to make -Count down Timer on entrance and exit triggers and display remaining time 0 Answers