Search
 
SCRIPT & CODE EXAMPLE
 

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

PREVIOUS NEXT
Code Example
Csharp :: how to set the forgound color of listitems in c# 
Csharp :: read barcode with barcode scanner c# winform serial port number 
Csharp :: how to combine cells in closedXML 
Csharp :: antlr c# errors 
Csharp :: xamarin forms uwp button hover 
Csharp :: c# half hour dropdown list 
Csharp :: how to get odd saturday in a month in c# 
Csharp :: c# hardcode datetime quoting 
Csharp :: two question marks together mean in C# 
Csharp :: missing integer c# 
Csharp :: unity c# script for movement left and right 
Csharp :: auto scroll infinite scroller unity 
Csharp :: c# words return first 20 items of array 
Csharp :: basic math functions in c# 
Csharp :: c# interoperability with linux or bash script 
Csharp :: string extentions not working 
Csharp :: remove language folders build visual studio 
Csharp :: Garbage collect every 30 frames unity 
Csharp :: nullable 
Csharp :: c# lambda get all records async 
Csharp :: detect mouse in bottom of screen + unity 
Csharp :: Close Form After fixed time 
Csharp :: jsonconvert serializeobject and jsonconvert deserialize to list 
Csharp :: asp.net render control to string 
Csharp :: Convert any class to a keyvaluepair 
Csharp :: create circumference with nettopologysuite 
Csharp :: get current culture in controller asp.net core 6 
Csharp :: C# write to lines without extra line 
Csharp :: UnitType parameter revit 2022 
Csharp :: model showing in scne view but not in game view 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =