Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Setting FX mixer volume by speed of a Rigidbody?

Discussion in 'Getting Started' started by Shin_Toasty, May 22, 2018.

  1. Shin_Toasty

    Shin_Toasty

    Joined:
    Jun 15, 2017
    Posts:
    48
    I've been trying to set FX volume in the main mixer by the speed of a Rigidbody. Below is code to affect a slider, (which is hidden offscreen) as I thought that might be simple, but no good it seems. And yes there is another FX slider in the in-game menu, (which works OK) so maybe that's part of the problem.

    I've no particular desire to use a slider, I just want the levels of all FX to drop/ raise according to the cube's speed.

    GameController.cubeSpeed is the speed variable and cubeVol does work out at somewhere between 0 and 1; that seems OK. But what parameter should go into MoveSlider() ?? I put 0.1f in and it saw no effect on the FX mixer; no errors in the console either.

    I hope there's a better way?

    Code (CSharp):
    1.  
    2.     public AudioMixer mainMixer;
    3.     public Slider fxSlider;
    4.     public float cubeVol;
    5.  
    6.     void Start ()
    7.     {
    8.         audioSource  = GetComponent<AudioSource> ();  
    9.     }
    10.  
    11.     public void MoveSlider (float cubeVol) {
    12.         fxSlider.value = cubeVol;    
    13.     }
    14.  
    15.     void Update (){
    16.         cubeVol= GameController.cubeSpeed / 100;
    17.         MoveSlider( what? );
    18.     }
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,965
    Just remove the function and move the code that sets the value into the Update() function.
    Code (CSharp):
    1. public AudioMixer mainMixer;
    2. public Slider fxSlider;
    3. public float cubeVol;
    4.  
    5. void Start ()
    6. {
    7.     audioSource  = GetComponent<AudioSource> ();
    8. }
    9.  
    10. void Update (){
    11.     cubeVol= GameController.cubeSpeed / 100;
    12.     fxSlider.value = cubeVol;
    13. }
    That's because the code is perfectly fine as far as the compiler is concerned. It's more of a logic error than anything and the compiler is not capable of understanding that's the case. Basically it's a problem with variable scope. I recommend the following reading but I'll try to explain it below.

    https://tutorialstown.com/csharp-variable-scopes/

    In your old code you have a variable declared in the class:
    Code (csharp):
    1. public float cubeVol;
    But by declaring a variable of the same name in the function parameter you were overriding the class variable:
    Code (csharp):
    1. public void MoveSlider (float cubeVol) {
    2.     fxSlider.value = cubeVol;
    3. }
    Because of that the value you were setting in Update() was literally being ignored:
    Code (csharp):
    1. void Update (){
    2.     cubeVol= GameController.cubeSpeed / 100;
    3.     MoveSlider( what? );
    4. }
     
    Last edited: May 22, 2018
    Shin_Toasty likes this.
  3. Shin_Toasty

    Shin_Toasty

    Joined:
    Jun 15, 2017
    Posts:
    48
    Thanks for answering so quickly Ryiah. :) I was just blind to line 3, I was too busy looking below it. Glad to see it was simpler than I thought.

    It does lock up the audio slider in the in-game menu though: can't drag it now, but I expected that; I can sort it out.

    The slider value actually has to be between -80 and 0, so my final formula is simply
    Code (CSharp):
    1. cubeVol = GameController.cubeSpeed - 25;
    because below -25 my imported audio files are silent.