I don't know how to increase the volume gradually after pressing the key.

Asked 2 years ago, Updated 2 years ago, 436 views

I would like to press KeyCode.W and gradually raise the AudioSource.volume to 3f.
The current code will freeze.

What should I do?

using System.Collections;
using System.Collections.General;
using UnityEngine;

public class PlayerManager:MonoBehavior
{
    private AudioSource audioSource;
    private float volumeWhile=3f;
  
    void Start()
    {
  
        audioSource=GetComponent<AudioSource>();
    }
    // Update is called once per frame
    void Update()
    {   
        if(Input.GetKey(KeyCode.W))
        {
            if(!audioSource.isPlaying)
            {
                while(audioSource.volume<volumeWhile)
                {
                    audioSource.Play();
                    audioSource.volume+=0.5f;

                    if(audioSource.volume==volumeWhile)break;
                }
            }
        }
        if(Input.GetKeyUp(KeyCode.W))
            {
            audioSource.Stop();
        }
    }
}

c# unity3d

2022-09-30 22:01

2 Answers

We compare == under the condition that the while statement is break, but since it is a floating point comparison, there may be a error in the process of calculating (increment here).

Instead, you might want to try "If the target value is exceeded."

 if(audioSource.volume>=volumeWhile)break;


2022-09-30 22:01

I don't know how to press [Unity] and then gradually increase the volume. has closed the problem using something that is not a routine, so I'll write it down here.The maximum value for AudioSource.volume is 1.

using System.Collections;
using UnityEngine;

public class PlayerManager:MonoBehavior
{
    SerializeField float_changeVolumeDelta = 0.001f;
    SerializeField float_changeVolumeIntervalSecond=0.1f;
    SerializeField float_maxVolume=1;
    AudioSource_audio=default;
    Coroutine_coroutine=default;

    void Start()
    {
        _audio=GetComponent<AudioSource>();
    }

    void Update()
    {
        if (Input.GetKeyDown (KeyCode.W))
        {
            // When the sound is not playing
            if(!_audio.isPlaying)
            {
                if(_coroutine!=null)
                {
                    _coroutine=StartCoroutine(IncreaseVolume(_maxVolume)));
                }
            }
        }

        // Turn down the volume while pressing ↓.
        if(Input.GetKeyDown(KeyCode.DownArrow))
        {
            _coroutine=StartCoroutine(DecreaseVolume());
        }
        else if (Input.GetKeyUp(KeyCode.DownArrow))
        {
            StopCoroutine(_coroutine);
        }
    }

    IEnumerator Increase Volume (float target)
    {
        while(_audio.volume<target)
        {
            _audio.volume+=_changeVolumeDelta;
            yield return new WaitForSeconds (_changeVolumeIntervalSecond);
        }
    }

    IEnumerator DecreaseVolume()
    {
        while(_audio.volume>0)
        {
            _audio.volume-=_changeVolumeDelta;
            yield return new WaitForSeconds (_changeVolumeIntervalSecond);
        }
    }
}


2022-09-30 22:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.