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 /
  • Help Room /
avatar image
0
Question by TobiKnitt · Feb 14, 2018 at 08:09 AM · audiofunctiondistancevolumerolloff

How does one code an audio rolloff along the x axis?

My 2D game requires a sound to have the same volume along the y axis, but a different volume depending on the distance to the camera on the x axis. I can't figure out how to code the last part of the script though, can you help me? The volume should stay at 0 until the distance is equal/smaller than MinDist at which point the volume should grow linearly until the distance is equeal/smaller than MinDist.

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     public class spatialFader : MonoBehaviour
     {
         private float pos;
         private float pos_other;
         private float dist;
     
         private AudioSource source;
     
         public float MinDist;
         public float MaxDist;
     
         private float sqrMinDist;
         private float sqrMaxDist;
     
         void Start ()
         {
             source = GetComponent<AudioSource> ();
             sqrMinDist = MinDist * MinDist;
             sqrMaxDist = MaxDist * MaxDist;
         }
     
         void Update () {
     
             pos_other = GameObject.Find("Main Camera").transform.position.x;
             pos = transform.position.x;
     
             dist = pos - pos_other;
             print("Distance to other: " + dist);
     
     //this is the part where I don't know what to do anymore:
             var t = 
             source.volume = Mathf.Lerp(0,1,t);
            
             }
     
     }
 
 
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
0

Answer by TobiKnitt · Feb 15, 2018 at 05:50 PM

I found a solution! Not what I originally had in mind, but for what I'm doing it works fine. At a certain distance it just fades in or out over time.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Audio;
 
 public class SoundRolloff : MonoBehaviour {
 
     private AudioSource source;
 
     public float spatialBlend;
     public float minDistance;
     public float maxDistance;
     public float fadeDistance;
     public float volume;
 
     private float pos;
     private float pos_other;
     private float dist;
 
     private float currentVolume;
 
 
     // Use this for initialization
     void Start () {
         source = GetComponent<AudioSource> ();
         source.volume = 0;
         currentVolume = source.volume;
     }
     
     // Update is called once per frame
     void Update () {
         source = GetComponent<AudioSource> ();
         source.spatialBlend = spatialBlend;
         source.minDistance = minDistance;
         source.maxDistance = maxDistance;
         source.dopplerLevel = 0;
         source.spread = 0;
 
 
         pos_other = GameObject.Find("Main Camera").transform.position.x;
         pos = transform.position.x;
 
         dist = pos - pos_other;
 
         if (dist < 0) {
             dist = -dist * 2;
         }
 
         if (dist > fadeDistance){
             source.volume -= volume * Time.deltaTime;
         }
 
         if ((dist < fadeDistance) && source.volume < volume) {
             source.volume += volume * Time.deltaTime;
         }
 
             
     }
 }
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

140 People are following this question.

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

Related Questions

change Volume of Audiomixergroups by script 1 Answer

Change audio volume of song depending on scene. 0 Answers

Unity custom Audio Manager won't work with custom Stop function 1 Answer

How do I store the current volume of an AudioMixer in a variable? 0 Answers

Volume Slider not saving when changing scene 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