Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

need help with slider decrease value with volume audiosource

Discussion in 'Scripting' started by djlevana18, Jan 17, 2021.

  1. djlevana18

    djlevana18

    Joined:
    Dec 11, 2020
    Posts:
    25
    i hard stuck 3 days now with this code i want my slider value decrease with volume of audiosource i tryed to work it out but i think i don't understand syntax between code so my solution was to change SliderInstance.Value to 0.01f but all this fix do is jumps up and down according audiosources origin volume but i want do this slider max value should be 100 and after time decrease by 99,98,97,96 etc and music of audiosource also should decrease until you will not able to hear it i want visual to match with hearing
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Music : MonoBehaviour
    7. {
    8.  
    9.  
    10.     // Start is called before the first frame update
    11.     public AudioSource AudioSource; // Assign in editor/etc
    12.     public float MusicVolume = 10f;
    13.     public Slider SliderInstance;
    14.  
    15.     public float Age = 2f;
    16.    
    17.     void Start()
    18.     {
    19.         AudioSource.Play();
    20.         SliderInstance.minValue = 0;
    21.         SliderInstance.maxValue = 100;
    22.         SliderInstance.wholeNumbers = true;
    23.         SliderInstance.value = 100f;
    24.     }
    25.     void Update()
    26.     {
    27.         Age += Time.deltaTime;
    28.         AudioSource.volume = .7f / (5.0f + Age / 1.0f);
    29.         SliderInstance.value =  100f *Time.deltaTime;
    30.         AudioSource.volume = SliderInstance.value;
    31.  
    32.     }
    33.     public void UpdateVolume(float Volume)
    34.     {
    35.         MusicVolume = Volume;  
    36.     }
    37.    
    38.  
    39.    
    40. }
    41.  
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
  3. djlevana18

    djlevana18

    Joined:
    Dec 11, 2020
    Posts:
    25
  4. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    Your current calculation will cause it to increase over time. Also dividing a number by 1 equals the original number its redundant.

    It would be simpler to do something like.
    Code (CSharp):
    1. AudioSource.volume -= audioDecreaseValue * Time.deltaTime;
    2. SliderInstance.value = AudioSource.volume;
    Keep your silders min max at 0 and 1 respectively.
     
  5. djlevana18

    djlevana18

    Joined:
    Dec 11, 2020
    Posts:
    25
    woooooow it worked so problem was when i messed around 0 and 1? next time i will respect that a little more i thought i can mess with whole numbers thank you a lot god bless u(if u believe so)