Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

unity play audio from particle system

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(ParticleSystem))]
public class CollidingParticles : MonoBehaviour {

    public AudioClip collisionSFX;

    ParticleSystem partSystem;
    ParticleCollisionEvent[] collisionEvents;

    void Awake () {

        partSystem = GetComponent<ParticleSystem>();
        collisionEvents = new ParticleCollisionEvent[16];
    }

    void OnParticleCollision (GameObject other) {

        int safeLength = partSystem.GetSafeCollisionEventSize();
        if (collisionEvents.Length < safeLength)
            collisionEvents = new ParticleCollisionEvent[safeLength];

        int totalCollisions = partSystem.GetCollisionEvents(other, collisionEvents);
        for (int i = 0; i < totalCollisions; i++)
            AudioSource.PlayClipAtPoint(collisionSFX, collisionEvents[i].intersection);

        print (totalCollisions);
    }
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #unity #play #audio #particle #system
ADD COMMENT
Topic
Name
6+7 =