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 andaleeni · Feb 21, 2012 at 12:11 PM · audioincreaselistenerpitchdynamically

Is it possible to raise an audio sources pitch as an audio listener gets closer?

Hi, I was wondering how you would attack this problem really. I would ideally like the sound the audio source is playing to increase as an audio listener object gets closer. I imagine a way to do this would be to tag the audio source as one thing and tag the listener as another then to check distance between the two.

I'm not sure what functions to use to do this though and I have had a good look around, there doesn't seem to be too much covering this topic. My problem is also made more difficult as I am not using graphics so no raycasting. I'm still fairly new to the coding but I have a decent level of understanding as I have been learning it for the past few months and have had a great success in programming my audio only game so far. All suggestions would be welcome!

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

5 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Muzz5 · Feb 21, 2012 at 02:26 PM

You could just use Audio.Pitch and Transform.Distance. It's pretty simple code, I'll write some for you later, if you need it.

EDIT: Seeing as you're a beginner, and I had some free time, I thought I'd write something up. Haven't tested this, though. Please tell me if there are any issues.

 var scaleFactor = 0.5;
 var object : Transform;
 
 function Update() {
     if(object)
     var dist = Vector3.Distance(object.position, transform.position);
         audio.pitch = dist * scaleFactor;
 }
Comment
Add comment · 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
0

Answer by zakkar · Feb 21, 2012 at 10:22 PM

It's automatically generate, your audio source should be a 3D Sound after that in the parameter you'll have to adjust the Min Distance and the Max Distance. You're sound will increase and decrease according to the parametre you have adjusted

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

Answer by jakovd · Feb 21, 2012 at 12:57 PM

You could add a script on a player with public GameObject variable i which you could (through editor) drag'n'drop that AudioSource object. In Update() method get source position through its transform and subtract that value from players.transform to get a vector between them. Getting length of that vector gives you distance between them and you can scale (multiply with some float value) that value and multiply with sources pitch to get the affect you want.

on the other hand, this behavior can be realistically achieved if you just add a sound as a 3D sound and let Unity simulate its damping automatically according to listeners position. i'm not sure if this is what you wanted because your post title said you want to change its "pitch" which is not the same as changing its "volume" (which is what you described in post text).

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

Answer by andaleeni · Feb 21, 2012 at 10:21 PM

Sorry for the confusion I was trying to convey what I have done so far, so yes I have used 3D sound method to convey a sense of the nearness through volume, my question was basically pitch related. From what I can tell then and from what I have found I need to find some way of subtracting both the audio listener and sound sources vector3s from each other, store it in a float and then call the gameobjects pitch component in audio source and tell it to move down and up according to the distance that the float calculating the difference between both objects vector3s are? Is this what you meant? Also if so is there an example of this you could give or point me to, to work this out code wise. I get the principle of what I have to do Its just putting it into practise with the correct code. Thanks for the fast reply. Also if you need an example of this behaviour the example I am trying to match is how FortressCraft handles being close or far away to a relic, the pitch increases the nearer the player gets to it. Hope that cleared up all the confusion!

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

Answer by andaleeni · Feb 21, 2012 at 10:21 PM

Yes that sounds like a good solution muzz id be very greatful if you could write me an example! Thanks so much for the reply!

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

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Networked Audio Listeners 2 Answers

sound is pitched down unexpectable 1 Answer

Separate Listeners for Sound FX and Music? 0 Answers

How to change pitch or volume on ALL audio sources in scene? 4 Answers

Multiple Audio Listeners for a split screen game. 2 Answers


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