How should I call my particle system (a small explosion) when my projectile hits enemy?,particle system not instantiating OnCollisionEnter
Hi, I have a particle system that I want to instantiate or play? I'm not sure which one maybe someone can advise. I want my particle system to show when my projectile bullet hits my enemy. My enemy is disabled back to the pool it came from in my EnemyShot() coroutine when my projectile hits the collider of my enemy. That is working, my enemy becomes disabled. But my particle system is not happening.
I have the instantate call for my explosion particel system in both my OnCollisionEnter function and my EnemyShot() function but neither seem to do anything at all... What's wrong?
btw this is my enemy script attached to my enemy prefab
public ParticleSystem explosion; public Transform explosionLocation;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Projectile"))
UpdateEnemyCountdown();
Instantiate(explosion, explosionLocation);
StartCoroutine(EnemyShot());
}
public IEnumerator EnemyShot()
{
Instantiate(explosion, explosionLocation);
ObjectPooler.Instance.DeactiveEnemy(gameObject);
yield return null;
}
}
,I have a bullet projectile and upon collision with an enemy I want it to call my particle system which is a little explosion.
The enemy is enabled from a pool then deactivated upon hit by the projectile (in a couroutine)
My attempt to call my particle system is not doing anything.
public ParticleSystem explosion; public Transform explosionLocation; ... ... ... private void OnCollisionEnter(Collision collision) { if (collision.gameObject.CompareTag("Projectile")) UpdateEnemyCountdown(); Instantiate(explosion, explosionLocation); StartCoroutine(EnemyShot()); }
.....
public IEnumerator EnemyShot() {
Instantiate(explosion, explosionLocation);
ObjectPooler.Instance.DeactiveEnemy(gameObject);
yield return null;
}
Any suggestions on how to get my particle explosion going? btw private void OnCollisionEnter(Collision collision) { if (collision.gameObject.CompareTag("Projectile")) UpdateEnemyCountdown(); Instantiate(explosion, explosionLocation); StartCoroutine(EnemyShot()); }
I have this enemy script on my enemy prefab (that is pooled, disabled upon EnemyShot()
Answer by HalilAlper · Apr 19 at 07:05 AM
Using OnCollisionEnter for triggering solutions would cause you wacky problems. You should try OnTriggerEnter instead, and form your stuff accordingly. I think this might work for you.
Instantiating for this stuff is bad practice. I would create an object pool inside my player and move the position and rotation accordingly.
Also make sure your particles' Looping is false so whenever you
play()
, it will play the explosion particle only once and I'm pretty sure that's what you want.
Your answer
Follow this Question
Related Questions
Particle System doesn t simulate in animation in editor 0 Answers
Gameobjects instantiating in the wrong position 0 Answers
Idiot can't play a particle system onCollisionEnter 2 Answers
Create particle onTriggerEnter for simulating a hit 0 Answers
How to make particles makes player feels about SPEED inside spaceship 0 Answers