- Home /
Question by
derobile42 · May 09, 2020 at 01:18 PM ·
instantiate2d gameparticle system
Instantiated Particle System seems to only be playing randomly and isn't being destroyed when it finishes.
I'm trying to get a particle effect to spawn when a bullet collides with a solid object, and then get destroyed after finishing. I've managed to the first part, but the thing is, it appears the particle effect is either playing fully, partially, or not at all. And as for being destroyed, that isn't happening.
Here's a video of it:
As you can see the effect does trigger, but only sometimes.
Here's the code for the bullet:
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using UnityEngine;
public class Bullet : MonoBehaviour
{
//bullet stuff
public float speed;
public int damage;
//how far the raycast shoots
public float distance;
//determines what layers the bullet will collide and therefore stop on
public LayerMask WhatIsSolid;
//partcile effect
public GameObject ps;
void Update()
{
//Moves the bullet forwards
transform.Translate(Vector2.right * speed * Time.deltaTime);
//Shoots a raycast forward at the bullets positition that reaches the speciied distance
//and only collides with the specified layers. It then assigns this information to an item called "hitInfo"
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.up, distance, WhatIsSolid);
//If the hitInfo collider is not empty or null
if (hitInfo.collider != null)
{
if (hitInfo.collider.CompareTag("Slime"))
{
//calls the TakeDamage function from the Script on the slime enemy
hitInfo.collider.GetComponent<EnemyController>().TakeDamage(damage);
}
StartCoroutine(bulletEffect());
//destroys the bullet game object
Destroy(gameObject);
}
}
IEnumerator bulletEffect()
{
print("Effect should play");
//should play the bullet effect at the bullets position
GameObject effect = (GameObject)Instantiate(ps, GameObject.Find("EffectSpawnPos").transform.position, Quaternion.identity);
//waits for the effect to finish
yield return new WaitForSeconds(0.3f);
//supposed to destroy the effect that has now finished
Destroy(effect.gameObject);
print("Effect should die");
}
}
And here's the Particle System settings:
That's all I'd imagine might be causing the problem but I'll be happy to give more if need be.
Cheers
005b-500x500.png
(50.7 kB)
untitled.png
(39.7 kB)
Comment