Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 GrayedFox · Apr 23, 2014 at 06:24 PM · soundparticlesystemdeath

Play sound on particle emit (sub emitter)

Hello,

I'm working on a simply script to play a sound effect when a particle system starts to emit particles. What I currently have works fine for particle systems but does not work for sub-emitted particle systems. I know why it doesn't work but I don't know how to get around this issue...

Goal: to play a sound - just once - when a particle system begins emitting particles (including sub emitted particle systems)

So far I have this:

 using UnityEngine;
 using System.Collections;
 
 public class ParticleSoundEffect : MonoBehaviour 
 {
 
     public AudioClip SoundEffect;
     public float PlaybackVolume = 1;
     public float PlaybackSpeed = 1;
 
     private bool effectPlayed = false;
     private float timeDelay = 0;
     private ParticleSystem parentSystem;
 
     void PlaySound()
     {
         if(SoundEffect && timeDelay <= 0 && effectPlayed == false)
         {
             effectPlayed = true;
             NGUITools.PlaySound(SoundEffect, PlaybackVolume, PlaybackSpeed);
         }
     }
 
     void Start() 
     {
         timeDelay = gameObject.particleSystem.startDelay;
         parentSystem = transform.parent.GetComponent<ParticleSystem>();
     }
 
     void Update() 
     {
         PlaySound();
         timeDelay = timeDelay - Time.deltaTime;
     }
 }

Now the reason this script doesn't work for sub emitters is that in my case (and I imagine in most people's) my sub emitter has a delay of 0, but of course does not play after 0 seconds - it plays as soon as the particle system that calls it dies (it is an 'on death' type sub emitter).

I could just add the lifetime of the emitted parent particles to the timeDelay variable however this would only work for particle systems that have a static particle lifetime and it still would only play the sound effect once.

The other method is to check if the particle count for a given particle system is greater than 0 (particleSystem.particleCount > 0) however this still only plays the sound once, and only when the first particle dies.

Anyone have an idea how I could make the attached sound effect play as soon as a particle system starts emitting - and every time the particle system starts emitting? Get/SetParticles might have to be part of it...

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

4 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by superdupergc · Oct 19, 2014 at 10:50 PM

I expanded the above answer into a script that can play a sound on particle birth as well. You may need to change the SoundManager.Play line into whatever you use.

 @script RequireComponent(AudioSource)
 
 public var OnBirthSound : AudioClip;
 public var OnDeathSound : AudioClip;
 
 private var _numberOfParticles : int = 0;
 
 function Update(){ 
     if (!OnBirthSound && !OnDeathSound){ return; }
     var count = particleSystem.particleCount;
     if (count < _numberOfParticles){ //particle has died
         SoundManager.Play(audio, OnDeathSound); 
     }else if (count > _numberOfParticles){ //particle has been born
         SoundManager.Play(audio, OnBirthSound); 
     }
     _numberOfParticles = count; 
 }

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
5

Answer by Danjeli · Apr 25, 2014 at 08:38 AM

My solution to this is to check the particle count in the system on update... if the particles are less then previous particles.. hence the particle has just died:

private void Update() { if (particleSystem.particleCount < _numberOfParticles) { //play Sound } _numberOfParticles = particleSystem.particleCount; }`

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
0

Answer by Laumania · Aug 21, 2019 at 12:02 PM

I go through my solution here - even though it's 5 years since the question was asked :)

https://www.youtube.com/watch?v=jKSz8JJnL4E

Enjoy!

Comment
Add comment · Show 1 · 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 Laumania · Nov 25, 2020 at 08:01 AM 0
Share

I created an updated version of my script and video, that actually also solves the position issue: https://www.youtube.com/watch?v=q_j$$anonymous$$iFHy93Y

avatar image
0

Answer by cwbeta · Nov 25, 2020 at 05:48 AM

If the particle system emits new particles slowly, you can check the particle count. If the count is less than previous count, it means some particles died.

However, you can not get the correct position of dead particles. If the system is always emitting new particles, the particle count may be always increasing and the solution cannot work.

If you need to know the particle position, you can try this:

        private void LateUpdate()
        {
            var count = _particleSystem.GetParticles(_particles);
            for (var i = 0; i < count; i++)
            {
                if (_particles[i].remainingLifetime >= 0 && _particles[i].remainingLifetime <= (isUnscaledTime ? Time.unscaledDeltaTime : Time.deltaTime))
                {
                    // Do something you want here
                }
            }
        }

This solusion may cause the sound to play 1 frame ahead, but you can use coroutine to delay 1 frame to play the sound. If your frame rate is stable, it will work perfectly.

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

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

24 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

Related Questions

Sound when dead 2 Answers

how do i make a sound effect play when the player dies 1 Answer

(SOLVED) [System.Serializable] / Inspector is not working 2 Answers

Raycast show ParticleSystem through an Array 0 Answers

Detect the start of a particle burst emission 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