- Home /
Sound play in spot of particle collision
Is there any way to create sound where particles of Particle System collides? Well, when a single particle comes into contact with the environment, it will be destroyed and a new one appears that should play the sound.
I create such a script responsible for playing audio - it is pinned to the 'Spark' object:
public int lastParticleCount = 0;
// Use this for initialization
void Update()
{
int newParticleCount = this.GetComponent<ParticleSystem>().particleCount;
if (newParticleCount > lastParticleCount) {
this.GetComponent<AudioSource>().PlayOneShot(GameObject.Find("Player").GetComponent<SoundManager>().sfxEmit[Random.Range(0, GameObject.Find("Player").GetComponent<SoundManager>().sfxEmit.Length - 1)]);
}
lastParticleCount = this.GetComponent<ParticleSystem>().particleCount;
}
Answer by xxmariofer · Oct 31, 2019 at 12:31 PM
i have not tested, but what i would do is use the https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnParticleCollision.html on particle collision event, in which you receive a reference to a game object, and you can play the collision on the position of that object but i am not sure how precise it will be since you receive the gameobject that collides rather than the collision so you cant get the collide contact point but something like this would be my very first approach
public AudioClip yourClip;
void OnParticleCollision(GameObject other)
{
AudioSource.PlayClipAtPoint(yourClip, other.transfor.position);
}
This is not what I mean - because now I gets objects that are floor or wall. If the returned GameObject were these particles it would be ok but it is not. I would need a position where the collision itself occurs, not the position of the object with which it interacts.
perhaps he means this:
public AudioClip yourClip;
void OnParticleCollision()
{
AudioSource.PlayClipAtPoint(yourClip,transform.position);
}
You can get a reference to the particle system itself, use getcollisionevents to get the collisions between the object and the particles
int numCollisionEvents = part.GetCollisionEvents(other, collisionEvents);
And iterate over the collisions for getting the intersection
Vector3 pos = collisionEvents[i].intersection;