Search Unity

HELP me pls!

Discussion in 'Scripting' started by leyenpa, Dec 18, 2014.

  1. leyenpa

    leyenpa

    Joined:
    Dec 18, 2014
    Posts:
    4
    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:
    file_01.jpg

    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!
     
  2. JobR

    JobR

    Joined:
    Jun 6, 2014
    Posts:
    14
    I don't actually know how to do the random part but the first part should be like
    Code (CSharp):
    1. void OnTriggerEnter (collider other)
    2. {
    3.      if(gameObject.tag == Particle)
    4. {
    5.      PlayClipAtPoint();
    6. }
    7. }
    Not sure about the playclipatpoint part :p

    the reason I use tagging is because I think is the easiest way but you choise :)
     
  3. Coldcalifornian

    Coldcalifornian

    Joined:
    Feb 11, 2014
    Posts:
    14
    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):
    1. using UnityEngine;
    2.  
    3. public class PlayRandomSoundOnParticleHit : MonoBehaviour {
    4.  
    5.     public AudioClip[] AudioClipsToPlayOnCollision;
    6.  
    7.     public void OnParticleCollision(GameObject other) {
    8.         audio.PlayOneShot(PickRandomSound());
    9.     }
    10.  
    11.     AudioClip PickRandomSound(){
    12.         int randomNum = Random.Range(0,AudioClipsToPlayOnCollision.Length);
    13.         return AudioClipsToPlayOnCollision[randomNum];
    14.     }
    15. }
    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.
     

    Attached Files:

  4. leyenpa

    leyenpa

    Joined:
    Dec 18, 2014
    Posts:
    4
    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? Bildschirmfoto 2014-12-22 um 17.57.01.png
     
  5. Coldcalifornian

    Coldcalifornian

    Joined:
    Feb 11, 2014
    Posts:
    14
    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).
     

    Attached Files:

  6. leyenpa

    leyenpa

    Joined:
    Dec 18, 2014
    Posts:
    4
    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... ;)