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): function OnTriggerEnter(other: Collider){ if (!audio.isPlaying){ audio.Play(); } } Could someone help me make a script that also emits a sound when the player leaves the box? Best wishes Andreas
It's almost identical, but OnTriggerExit: http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerExit.html
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.
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!
I'm dying to know: How would the code look if I want both an OnTriggerEnter and an OnTriggerExit in one script?
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.
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
Hi Andreas. You can have multiple functions in one script. It would simply look something like this: Code (CSharp): function OnTriggerEnter(other: Collider) { if (!audio.isPlaying){ audio.Play(); } } function OnTriggerExit(other: Collider) { //do something else }
Limeoats has a good answer, but since I wrote this up I'm going to go ahead and post it anyway. =P Code (JavaScript): var audioEnter : AudioClip; var audioExit : AudioClip; function OnTriggerEnter(collidingObject : Collider) { audio.PlayOneShot(audioEnter,1.0); } function OnTriggerEnter(collidingObject : Collider) { audio.PlayOneShot(audioEnter,1.0); } 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): var audioVolume : 1.0; function OnTriggerEnter(collidingObject : Collider) { audio.PlayOneShot(audioEnter,audioVolume); } 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. =)
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
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): var audioEnter : AudioClip; var audioExit : AudioClip; function OnTriggerEnter(collidingObject : Collider) { audio.PlayOneShot(audioEnter,1.0); } function OnTriggerExit(collidingObject : Collider) { audio.PlayOneShot(audioExit,1.0); } Also thanks for the links to the Scriptreferences, Barachiel - I'll definitely delve into that! And thanks also to Cpt Chuckles for advice!