Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by nesterio · May 11, 2021 at 11:19 AM · 2dparticles2d-platformerparticlesystemparticle system

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 alt text

 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();
             
         }
 
     }
 }


ezgif-7-621cda61ebd1.gif (479.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nesterio · May 11, 2021 at 12:09 PM 0
Share

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

avatar image Koyemsi nesterio · May 11, 2021 at 12:42 PM 0
Share

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...

avatar image nesterio Koyemsi · May 12, 2021 at 09:44 AM 0
Share

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: alt text

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?

screenshot-2021-05-12-at-124345-pm.png (15.5 kB)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

289 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Particle System: How To Control Axis? 0 Answers

Make Start Color setting in particle system control _EmissionColor 1 Answer

Blast Particle Gets Cut Off 0 Answers

How to destroy particle gameobject after its finished? 2 Answers

In which order Unity ParticleSystem's modules execute? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges