• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Aliasmk · Sep 13, 2010 at 05:41 PM · audiotriggersound

Get an object to play a sound if player rolls over a trigger.

enter code hereHi everyone. I am trying to get a sound to play when the player rolls over a seperate trigger. For this i have two scripts.

script 1 is attached to the trigger.

static var istriggered = false;
function OnTriggerEnter ()
{
   istriggered = true; //sends to script 2
}

script 2 is attached to the speaker object.

var audio = audioClip
function Update ()
{
   if(script1.istriggered == true)
   {
        audio.play("audio");
   }
}

it doesnt work properly, i hear a ghost sound even before the player rolls over the trigger. Any help is appreciated. Thanks.

Comment

People who like this

0 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jesse Anders · Sep 13, 2010 at 06:02 PM 0
Share

I'm not sure why you'd hear a sound before the player touches the trigger; for that, you might add some debug output in your OnTriggerEnter() function to see whether the two events are in sync or not. That said, as is, your code will simply attempt to start playback of the sound repeatedly once the trigger has been entered. Try adding the line 'script1.istriggered = false' before or after the line 'audio.play("audio");', and see if that makes any difference.

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by jtbentley · Sep 13, 2010 at 11:19 PM

That will trigger constantly once that boolean is set to true. Either add an additional boolean inside your code here...

function Update ()
{
   if(script1.istriggered == true && !hasTriggered)
   {
        audio.play("audio");
        hasTriggered = true; // So it'll only play once - you might then want to release this again
   }
}

However, I think you'd be better off just calling a routine to play the sound...

function OnTriggerEnter () { if (!hasTriggered) playSound(); // Only play if the trigger has been released }

function playSound() { audio.PlayOneShot(whatever); yield WaitForSeconds(timeUntilCanPlayAgain); hasTriggered = false; }

Comment

Aliasmk

People who like this

1 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Aliasmk · Sep 14, 2010 at 05:27 PM 0
Share

Thanks for this, im sure that this will work to solve my problem.

avatar image

Answer by · Sep 13, 2010 at 11:28 PM

You don't need to store a static variable on the trigger script, and constantly check if it's true on the speaker script. As Jesse Anders pointed out, your code will continue to play the sound. All you need to do:

Script 1 (attached to the trigger)

var speaker : Transform;

function OnTriggerEnter () { if ( speaker && !speaker.audio.isPlaying ) speaker.audio.Play(); }

Link the speaker object to the script on the trigger object, and it will check if the sound is already playing before setting it to play.

As for hearing a 'ghost sound' before the player "rolls over the trigger" - I assume the ghost sound is the audio you want to play? OnTriggerEnter will fire when the Colliders touch at all (i.e. when the exterior bounding box of the player intersects with the furthest extent of the trigger box). You could add a Debug.Log("INSIDE THE TRIGGER"); to your OnTriggerEnter to see exactly when that occurs.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Checking collision between 2 triggers based on distance. Possible? 1 Answer

Activate sound without Pro filters 0 Answers

Door Opens When Not In Trigger And Sound Help 1 Answer

Trigger Sound to Play Globally Not on Triggered Object 1 Answer

A node in a childnode? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges