• 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
0
Question by CowsMilkYou · Apr 08, 2012 at 03:08 PM · javascriptsoundnearplays

Sound plays once close to an object

I've seen a lot of scripts that play a sound once you collide with an object, but I need one that makes a sound play once you come close to a specific object. I also need it to work on multiple objects throughout one level. Can someone help me with a script?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by fafase · Apr 08, 2012 at 03:25 PM

You can compare the distance between the player and the object with distance() function and then when the guy gets in the range, play the sound.

 var other : Transform;
 var range:int;
 var sound:AudioClip;
 var played:boolean;
 var timing:int;
 var delay:int=10;
 
 function Start(){
 played=false;
 timing =Time.time;}
 
 function Update(){
 if (Vector3.Distance(other.position, transform.position)<range) {
       if(timing<Time.time){
            played = true;
            timing = Time.time + delay;}
   }
 if(played){
 AudioSource.PlayClipAtPoint(sound, transform.position);
 played=false;
 }
Comment
Add comment · Show 17 · 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 CowsMilkYou · Apr 08, 2012 at 07:55 PM 0
Share

Thank You! This helps so much! Is this a way to make it so the sound dosen't loop?

avatar image DJSwiti · Apr 08, 2012 at 08:03 PM 0
Share

Yes, just add in if(played) : audio.loop = false

avatar image CowsMilkYou · Apr 09, 2012 at 04:48 AM 0
Share

I'm having trouble implmenting the audio.loop = false into the script. After messing with errors for 10 minutes I was led to if(played){ audio.loop = false; at the end of the script, but that still continued to loop the sound. I would I implement if(played) : audio.loop = false into the scipt?

avatar image fafase · Apr 09, 2012 at 06:35 AM 0
Share

No the pb comes frm the fact that played gets back to true right on. $$anonymous$$y mistake. I updated with a timing. Now the sound will happen every 10 sec.

avatar image Hades714 · Oct 26, 2012 at 03:49 PM 0
Share

This script is AWESO$$anonymous$$E...Is it posible to add if you spot or look at enemy then it plays the sound.I was trying to set it,I have even checked tutorials but I can't get it right.I must be doing something wrong.If you find the way to set it,could you please post a complete script-it would mean a lot to me.-thanks (sorry for my bad English)

Show more comments

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Impact Sound When Jumping? (JavaScript) 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Enemy sound detection 5 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges