- Home /
OnParticleCollision and Play Audio
I have a particle system that is colliding with a plane. The particle system will be water and I want it to make a drip sounds when the particle collides with the plane. I've turned on Send Collision Message, I have the basic example OnParticleCollision script that Unity provides, but I'm not sure where to add PlayAudio source? within the sample script Unity provides. Any help would be great thank you!
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void OnParticleCollision(GameObject other) {
Rigidbody body = other.GetComponent<Rigidbody>();
if (body) {
Vector3 direction = other.transform.position - transform.position;
direction = direction.normalized;
body.AddForce(direction * 5);
}
}
}
Answer by Cycy · Oct 23, 2017 at 08:51 AM
Hello,
wherevere you want :) the OnParticleCollision is called when a collision is detected. So it's where you want, but if you write it in your "if" condition make sure to enter in it to play the sound.
Answer by Sgt_Gemini · Oct 23, 2017 at 10:24 AM
If I understand your question correctly you are asking for help on how to make the sound play. As Cycy suggested you can make it play in the OnParticleCollision as shown below.
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public AudioSource mySound;
void OnParticleCollision(GameObject other) {
Rigidbody body = other.GetComponent<Rigidbody>();
if (body) {
Vector3 direction = other.transform.position - transform.position;
direction = direction.normalized;
body.AddForce(direction * 5);
}
mySound.Play();
}
}
Of course you need to make sure you have an AudioSource somewhere and that you assign it to the mySound variable in the inspector.