Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 straydogstrut · Apr 02, 2010 at 10:57 PM · audiosourcevolumerolloff

Change variable value based on perceived audio volume

If we know that Unity uses the calculation volume = 1.0 / (1.0 + rolloff * (distance 1.0)); to calculate volume based on distance, is it possible to use this in some way to manipulate the value of a variable based on the current 'perceived' volume? Such as changing the alpha value of a GUITexture's color.

I half-suspect the answer is a flat 'no', but if we know the rolloff value, can't we use this in some way? I guess an easier way is to fake it by checking the player's distance to the audiosource or using triggers, but that would surely mean a lot of tweaking the radius to match up with the sound rolloff. Just curious if there's another way.

Promise this will be my last question of the day!

Thanks.


[EDIT] I've been playing around with the formula to see what I can get from it, but i'll be honest and say i'm not 100% sure what i'm doing here. I couldn't get the right syntax to decalre a variable with this formula on a single line, so i've attacked it in two parts, then combined the result. The value returned is too large to be useful (ie, above 1.0) so I need a way to convert it to a value between 0 and 1 (I believe Mathf.Clamp is what i'm looking for but using Mathf.Clamp((1.0 / rolloff * dist),0.0,1.0); doesn't map it between 0.0 and 1.0). As I said, i'm fumbling around in the dark here so excuse my ignorance!

var player : GameObject; var rolloff : float;

function Start(){

// temporary, i'll assign these from the editor later audioSource = gameObject.Find("Sphere"); player = gameObject.Find("First Person Controller");

}

function Update(){

//the (distance - 1.0) part of the equation var dist = Vector3.Distance(audioSource.transform.position, player.transform.position) -1.0;

// the 1.0 + rolloff part of the equation rolloff = 1.0 + audioSource.audio.rolloffFactor;

// combining the two currentVol = 1.0 / rolloff * dist;

guiTexture.color.a = currentVol;

print(currentVol);

}

Comment
Add comment · Show 3
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 straydogstrut · Apr 03, 2010 at 12:38 AM 0
Share

I can confirm that the above does have an effect so long as I am close to the object (ie, while currentVol is a float less than 1.0). It's constantly changing because the sphere is moving, but is definitely changing the alpha of the GUITexture as I get closer (it's actually fading out the closer I get so I might have something back to front). Still need to figure out how to map currentVol to a value between 0.0 and 1.0 however.

avatar image Jessy · Apr 03, 2010 at 01:10 AM 0
Share

You're not going about this quite right anyway. The volume is NOT the "perceived volume". Humans don't hear on a linear scale. You can check out http://www.unifycommunity.com/wiki/index.php?title=Listener for something that will match better. I'd put some effort into helping you, but I think Unity 3.0 is going to make all this boring fixing of the audio curves unnecessary, so I'm waiting for that version to see what happens. Hopefully everything I put on the wiki becomes obsolete.

avatar image straydogstrut · Apr 03, 2010 at 11:53 AM 0
Share

Hi Jessy, I know that volume is not the perceived volume, and therein lay my problem: trying to get some representation of the perceived volume which I can use to alter the value of something like a GUITexture's alpha. I found your scripts on Unify yesterday but i'll be honest, all this is over my head so I wasn't sure how I could use them to help me here. I do appreciate that it's meaningless to find an answer here since Unity 3.0 is on the way, but my project will be completed before then. However, I might just change the alpha based on distance for simplicity. Thank you for your insights.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Lucas Meijer 1 · Apr 02, 2010 at 11:14 PM

If you know that that is the formula, nothing is stopping you from running the same formula, and doing something with the resulting value.

You can ask as many questions on a day as you like :)

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 straydogstrut · Apr 03, 2010 at 12:22 AM 0
Share

Hi Lucas, i'm trying to do that but I wasn't able to combine the entire formula on a single line. I've amended my question above to show you what i've got.

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

No one has followed this question yet.

Related Questions

webgl volume just doesnt work 0 Answers

How to get current volume of an AudioSource. 3 Answers

Audio Volume Help 1 Answer

cannot implicitly convert type 'int' to 'bool' problem 1 Answer

Need help with AudioSources 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