Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Velketor · Jun 05, 2012 at 07:51 PM · playervolumegame objectproximity

Adjust audio based on Proximity

How can I adjust audio volume based on how close the player gets to a game object?

Please let me know. Thank you!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by stingman · Jun 05, 2012 at 09:05 PM

define your player's distance from the object and tell it to play when he gets within range:

for instance... something like this would be attached to your game object. Make sure your game object has an audio source on it and a clip attached.. otherwise assign a clip in code. I'll do that here...

 var playerDistance : float;
 var gameAudio : AudioClip;
 var canPlayAudio = true;
 
 function Update() {
 
 playerDistance = player.transform.position - transform.position;
 
 if (playerDistance < 10) {
 if (canPlayAudio) {
 audio.clip = gameAudio;
 audio.volume = 0.5; // adjust the volume to whatever you want
 audio.Play();
 canPlayAudio = false;  // you can reset this in your code elsewhere... I did this just to make sure the audio only plays once.
 }
 }
 }
Comment
Add comment · Show 2 · 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 Velketor · Jun 05, 2012 at 11:05 PM 0
Share

Thank you for the help! I tested this code out also and it say player is not defined as a variable so i added

var player;

and it stopped the errors but i don't think the script is actually doing anything because my sound plays exactly the same when I'm close to the gameobject and when I'm far from the gameobject. Unfortunately Unity's 3D sound isn't giving me the effect I'm looking for. $$anonymous$$aybe I'm doing something wrong? Please help if you can. i appreciate it!

avatar image stingman · Jun 06, 2012 at 05:09 PM 0
Share

yes sorry I should have noted this is just an idea to work off of. Post what you currently have and let me know exactly what you want to happen... specifics and I'll get it working for you. JS or C# is fine

avatar image
0

Answer by GutoThomas · Jun 05, 2012 at 09:05 PM

Well, that's the point of a 3D sound. Sounds quite when far from the listener and loud when next to it. But, if you want to do this by script you can add a script to the gameobject which has an audio source and do some coding like:

C# code:

     public Transform target;
     
     void Start () {
     
     StartCoroutine(AdjustVolume());
     
     }
     
     IEnumerator AdjustVolume () {
     
     while(true) {
 
 if(audio.isPlaying) { // do this only if some audio is being played in this gameObject's AudioSource
 
     float distanceToTarget = Vector3.Distance(transform.position, target.position); // Assuming that the target is the player or the audio listener
     
     if(distanceToTarget < 1) { distanceToTarget = 1; }
     
     audio.volume = 1/distanceToTarget; // this works as a linear function, while the 3D sound works like a logarithmic function, so the effect will be a little different (correct me if I'm wrong)
     
     yield return new WaitForSeconds(1); // this will adjust the volume based on distance every 1 second (Obviously, You can reduce this to a lower value if you want more updates per second)
     
     }
     }
     
     }
Comment
Add comment · 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 Velketor · Jun 05, 2012 at 11:03 PM 0
Share

Thank you for your help! I tried out the C# code and it gives me an error on the first line

public Transform target;

Thoughts?

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

[C#] how do I get the name of the object under the player? 1 Answer

Player Proximity Detection 1 Answer

How to make volume continually go down in C#Script ? 1 Answer

Making a GUI texture fade in and out depending on player proximity with enemy 2 Answers

Move the enemy object opposite to player and keep its distance relative to the player? 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