- Home /
Particles don't always play
Hey everyone! I have made a simple script to emit particles every time player bounces from bouncy platforms. Everything works just fine, but particles don't emit every time, the player bounces of this platform. I have tried using the "Play()", but particles were simply too slow, to fully play, before they are reset.
Here is a video of how this looks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bouncy_Script : MonoBehaviour
{
public float bouncyness = 2200;
private Animation anim;
private CharacterController2D CC2D;
public GameObject particles;
void Start(){
CC2D = GameObject.Find("Player").GetComponent<CharacterController2D>();
anim = GetComponent<Animation>();
}
void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.CompareTag("Player")){
CC2D.Bounce(bouncyness);
CC2D.m_AirControl = true;
Instantiate(particles, transform.position, transform.rotation);
anim.Play();
}
}
}
Answer by nesterio · May 13, 2021 at 08:56 AM
Well, guess what... After different approaches and many tries I have figured out that the reason why the particles did not always appear, was simply because their sorting layer in renderer was 0...
Answer by Koyemsi · May 11, 2021 at 11:38 AM
If I understand well, you're instantiating a new particle system for each bounce. I'm not sure this is a good idea, for performance reason (garbage collector). I would think preferable to embed your PS inside your platform prefab, register it and call its Play() method when needed (just as you do it for anim).
Then, the reason why it isn't playing each time is probably because of your PS settings, just like you said. Consider lowering its Duration and Start lifetime.
Hope this will help.
Thank you for your answer! I delete the particle prefab I instantiate, after it's done, so I don't think it will do too much harm. The Play() method doesn't work here, as it resets the particles. I don't want to make particles last less, because it won't look as good as it looks now. So, Im still looking for a solution
You're welcome. Concerning the instantiation, you may be right as you're destroying your PS after it has played, so it is possible you don't notice any slowing effect on your global performance. But I've read several times that continual instantiation can have a heavy cost in terms of memory allocation (creates a lot of garbage). That's why it's always better to re-use objects as far as possible, rather than destroying and creating new ones (you should read about "pooling"). I assume your levels contain a lot of these lovely bouncy platforms, so this may be of interest in your case.
Now, concerning your fancy effect, maybe you could play with your PS emission parameters. I would try something like looping it (disable Start at awake), turning its rate emission to zero, and changing its Burst rate dynamically within your script (so that your PS emits a burst of particles when player bounces). Rather than calling Play() at bounce, you would make it emit a burst, keeping your particles lifetime the value you want. No more instantations to do, you just have 1 PS per platform playing continuously, but emitting only when needed. Didn't try this myself but I think such a solution could solve your problem and prevent garbage collection in a same time. Please let me know if that helped...
So, I tried doing as you said, but I am having difficulties controlling the emission rate via script. I am doing exactly as Unity documentation tells me to. link text Using this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bouncy_Script : MonoBehaviour
{
public float bouncyness = 2200;
private Animation anim;
private CharacterController2D CC2D;
public ParticleSystem particles;
private ParticleSystem.EmissionModule emissionModule;
public float emissionAmount = 10;
public float emissionLength = 0.8f;
private float TimeLeft;
private bool emitting = false;
void Start(){
CC2D = GameObject.Find("Player").GetComponent<CharacterController2D>();
anim = GetComponent<Animation>();
particles = GetComponent<ParticleSystem>();
emissionModule = particles.emission;
TimeLeft = emissionLength;
}
void Update(){
//var emission = particles.emission;
if(emitting){
TimeLeft -= Time.deltaTime;
}
if(TimeLeft > 0 && emitting){
emissionModule.rate = emissionAmount;
}
else if(TimeLeft <= 0){
emitting = false;
emissionModule.rate = 0;
TimeLeft = emissionLength;
}
}
void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.CompareTag("Player")){
CC2D.Bounce(bouncyness);
CC2D.m_AirControl = true;
emitting = true;
//Instantiate(particles, transform.position, transform.rotation);
anim.Play();
}
}
}
But I am getting this error:
NullReferenceException: Do not create your own module instances, get them from a ParticleSystem instance UnityEngine.ParticleSystem+EmissionModule.set_rateOverTime (UnityEngine.ParticleSystem+MinMaxCurve value) (at :0) UnityEngine.ParticleSystem+EmissionModule.set_rate (UnityEngine.ParticleSystem+MinMaxCurve value) (at /Users/bokken/buildslave/unity/build/Modules/ParticleSystem/Managed/ParticleSystem.deprecated.cs:33) Bouncy_Script.Update () (at Assets/Scripts/Obj/Bouncy_Script.cs:42)
Do you know how can I fix this?