Search Unity

Audio Trigger Script

Discussion in 'Scripting' started by Andreas Busk, Sep 30, 2014.

  1. Andreas Busk

    Andreas Busk

    Joined:
    Mar 21, 2014
    Posts:
    25
    Hi all,

    I've just started doing audio triggers - player moves through a box collider with trigger on and triggers audio source with this script:

    Code (JavaScript):
    1. function OnTriggerEnter(other: Collider){
    2.   if (!audio.isPlaying){
    3.     audio.Play();
    4.   }
    5. }
    Could someone help me make a script that also emits a sound when the player leaves the box?

    Best wishes

    Andreas
     
    Last edited: Sep 30, 2014
  2. DarkArts-Studios

    DarkArts-Studios

    Joined:
    May 2, 2013
    Posts:
    389
  3. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    You should be able to do that the same way, only using OnTriggerExit() instead.

    By the way, just so's you don't get in trouble for not reading the rules on code formatting in the forum, you should probably give this a quick read:

    http://forum.unity3d.com/threads/using-code-tags-properly.143875/

    Code formatting using the tags makes it easier for everyone to read the code. =)

    EDIT: Woops, beaten to the punch.
     
  4. Andreas Busk

    Andreas Busk

    Joined:
    Mar 21, 2014
    Posts:
    25
    Thanks for the correction, I'm new to scripting and this forum. I've edited my post now - the right way?

    Also thanks a lot for the advise you two - it works now!
     
  5. Andreas Busk

    Andreas Busk

    Joined:
    Mar 21, 2014
    Posts:
    25
    I'm dying to know: How would the code look if I want both an OnTriggerEnter and an OnTriggerExit in one script?
     
  6. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    Your code formatting looks great now. =)

    You can have OnTriggerEnter and OnTriggerExit in the same script just fine. It'll call one function on entering the trigger zone and the other on exiting. You can also have OnTriggerStay for when you want to detect something continuing to be in contact with the trigger.
     
  7. Andreas Busk

    Andreas Busk

    Joined:
    Mar 21, 2014
    Posts:
    25
    I am still in the first few days of watching/trying to grasp code in the entirety of my life. Could you show me an example of using OnTriggerEnter and OnTriggerExit in the same script?
    Best,
    Andreas
     
  8. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    Hi Andreas.

    You can have multiple functions in one script. It would simply look something like this:

    Code (CSharp):
    1. function OnTriggerEnter(other: Collider) {
    2.   if (!audio.isPlaying){
    3.     audio.Play();
    4.   }
    5. }
    6.  
    7. function OnTriggerExit(other: Collider) {
    8.   //do something else
    9. }
     
  9. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    Limeoats has a good answer, but since I wrote this up I'm going to go ahead and post it anyway. =P

    Code (JavaScript):
    1. var audioEnter   :   AudioClip;
    2. var audioExit   :   AudioClip;
    3. function OnTriggerEnter(collidingObject : Collider)
    4. {
    5.    audio.PlayOneShot(audioEnter,1.0);
    6. }
    7. function OnTriggerEnter(collidingObject : Collider)
    8. {
    9.    audio.PlayOneShot(audioEnter,1.0);
    10. }
    So the first thing is the audio clips. With this script, once saved (and no errors have occured) you'll find audioEnter and audioExit in the inspector panel on whatever object you have this script assigned to. From there, you just drag and drop your audio clips into the appropriate locations.

    With the OnTrigger functions, in the brackets after that where I have the collidingObject variable, that'll be whatever trips the trigger. We're not using that in this script, but it still needs to be assigned.

    Inside the functions we have audio.PlayOneShot. In the brackets is the name of the variable we declared earlier that holds the clip you want to play, followed be a float number. The number is the volume the clip will be played at, with 1.0 being it's loudest, and 0.0 being mute. You could have a float variable and use that instead, such as:

    Code (JavaScript):
    1. var audioVolume   :   1.0;
    2. function OnTriggerEnter(collidingObject : Collider)
    3. {
    4.    audio.PlayOneShot(audioEnter,audioVolume);
    5. }
    audio.PlayOneShot is only one way of playing audio of course.

    For more on audio related stuff: http://docs.unity3d.com/ScriptReference/AudioSource.html

    And for collider related stuff: http://docs.unity3d.com/ScriptReference/Collider.html


    The docs are an amazing place if you need any help, probably the first place to look if you need anything. If you were after info about animating, for example, just type that in to the docs search bar and you'll find a wealth of info. Same goes for just about any other topic.

    Of course, no harm in asking for help around here as you already have, the docs just have a more immediate response. =)
     
  10. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    Haha, good idea. Your answer is more detailed anyway :p
     
  11. Cpt Chuckles

    Cpt Chuckles

    Joined:
    Dec 31, 2012
    Posts:
    86
    if you are brand new to coding, i think it would be greatly beneficial to go through the MSDN C# programming guide while playing with Unity, because it will help you learn how to program, and how to do it in C# which in certain cases is vastly superior to UnityScript. combine that with combing Youtube for unity tutorials done in C# (PushyPixels has tons of videos) and recreating what they make, changing things, experimenting, etc. and after a while you will grasp the background mechanics of why things are the way they are
     
  12. Andreas Busk

    Andreas Busk

    Joined:
    Mar 21, 2014
    Posts:
    25
    Limeoats and Barachiel: Thanks for your detailed and very useful guidance!

    I got your script working after editing it, Barachiel - in your line number 7 and 9 you wrote 'enter' instead of 'exit' I think - here's my edit of your code. This works as you described it would :)

    I'm very happy! Thanks!

    Code (JavaScript):
    1. var audioEnter   :   AudioClip;
    2. var audioExit   :   AudioClip;
    3. function OnTriggerEnter(collidingObject : Collider)
    4. {
    5.    audio.PlayOneShot(audioEnter,1.0);
    6. }
    7. function OnTriggerExit(collidingObject : Collider)
    8. {
    9.    audio.PlayOneShot(audioExit,1.0);
    10. }
    Also thanks for the links to the Scriptreferences, Barachiel - I'll definitely delve into that!

    And thanks also to Cpt Chuckles for advice!
     
  13. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    Woops, posted that late at night, my bad. Glad to be of help all the same. =)
     
  14. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    Glad to see you get it working!