Hey there, i was wondering if you can help me with a very simple script. – well, very simple for you. I am an absolute beginner comming more from the design devision. At the moment i am doing my bachelor thesis in Design. Here is my scene: The small spheres (Particles) are moving very slow into the big sphere... I would love to have a collision-sound, if they hit the sphere. I need something like: if Particles hit the sphere (big in the middle) play random sounds (out of 3)... i would love to have 3 var for 3 different sounds, 1 var for the Particle Emitter and 1 var for the hitting Object (big sphere) so that i cant destroy something in the script Do they particles need anything more? - for example a collider? Pls help me! I need you!
I don't actually know how to do the random part but the first part should be like Code (CSharp): void OnTriggerEnter (collider other) { if(gameObject.tag == Particle) { PlayClipAtPoint(); } } Not sure about the playclipatpoint part the reason I use tagging is because I think is the easiest way but you choise
I recommend you don't use the OnTriggerEnter method because it will require your particles to have colliders (inefficient). Alternatively, Unity has a build in message call specifically designed for your scenario. Make sure to read the following section to get a full explanation of when this message is called (you will be using the Shuriken particle system if you're up to date with unity 4.6). http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnParticleCollision.html Here is some code which responds to this message: Code (CSharp): using UnityEngine; public class PlayRandomSoundOnParticleHit : MonoBehaviour { public AudioClip[] AudioClipsToPlayOnCollision; public void OnParticleCollision(GameObject other) { audio.PlayOneShot(PickRandomSound()); } AudioClip PickRandomSound(){ int randomNum = Random.Range(0,AudioClipsToPlayOnCollision.Length); return AudioClipsToPlayOnCollision[randomNum]; } } If you attach this script to the big sphere, your particle system will be able to communicate with it, and you can have a random sound play. But your not finished yet... On the particle system itself under the collision section, make sure you have the world option selected and the Send Collision Messages box check. Read the Collision Module section on this page for more details http://docs.unity3d.com/Manual/class-ParticleSystem.html One last thing, your big sphere will need an AudioSource component. Make sure you uncheck play on awake! Hope that helps! P.S. I'ld love to see the finished product. Looks like a fun project.
Hey, thank you very much! I did everything like Coldcalifornian wrote but it gets me an Error: "The referenced script on this Behaviour is missing"... Here is the pasted code: Should i add anything or fill anything in?
I don't think that error is being caused by the new play sound script. That error is frequently thrown when you are using a script on a GameObject and accidentally (or deliberately) delete the referenced script file from your project. Here is a good thread on the topic. Take a look at the script on the wiki. http://answers.unity3d.com/question...ed-script-on-this-behaviour-is-missing-2.html You need to look through all the GameObjects in your scene for something that looks like the picture (not the easy code scanner part).
Hey there, thank you very much. It works now! There is only one problem: If two particles collide on the same time or during an other collision it plays only 1 sound and waits until its finished. Is there a possiblity to play a sound with every collision, even if it plays an collision sound allready? I hope you can help me one more time...