Reputation: 35
Why is this happening and how can I make it stop?
Like the title says, sometimes pressing A/D on my keyboard while playing my game adjusts the volume, when I just want A/D to move my player character left and right and have no effect on the volume.
Background Information: I followed this tutorial to setup my audio volume slider UI element : https://www.youtube.com/watch?v=YOaYQrN1oYQ&t=122s
Here is the code for my volume slider:
public class MasterAudioSlider : MonoBehaviour
{
public AudioMixer audioMixer;
public void SetVolume(float volume) => audioMixer.SetFloat("volume", volume);
}
I have four UI buttons set up so that when each one is clicked it cycles through toggling groups of audio sources on/off, cycling back around to the first group of audio sources. I can post the code if further clarification is needed.
Here's what I know so far (please excuse the formatting, it's supposed to be a single-level bulleted list):
- Pressing A decreases the volume, D increases it.
The left and right arrow keys also have the same effect, as well as any other keys that are mapped to the left and right character movement.
If I press A or D while W or S is already down, the volume does not change. This only happens with W/S already down.
- Whenever the slider element or the audio mixer itself is disabled/disconnected, the problem stops.
In my code, changing it from source.Pause() to source.Stop() changes nothing.
Just pausing the game (and consequently enabling my pause menu) does not trigger the problem. Clicking around in the pause menu doesn't trigger the problem, either. It only happens when a UI element has been clicked (maybe because some value or state has been changed).
Getting the problem itself to occur is kind of finicky. I'll try my best to explain.
When the game starts, I already have some audio sources set to play on awake, and the problem doesn't occur until I click one my buttons to start playing my other audio sources. Once it cycles back around to playing the first audio sources, the problem persists.
Changing the volume of my audio with my slider also seems to trigger the problem even if I haven't clicked the button to play the new audio sources.
The problem seems to start once the player has moved into the minimum range of the audio source falloff and persists even when they move out of it into the maximum range. EDIT: this is not the case as it is possible to trigger the problem while standing in the maximum range and never stepping into the min. It seems to take a bit longer after pressing W/S, but it does happen.
If the player has stepped into the minimum falloff range, it doesn't happen until either W or S is pressed before press A or D is pressed. No other keys seem to have this effect.
If the player is standing in the minimum range when the audio sources are changed, the problem doesn't occur until W/S is pressed again.
Sometimes it'll change the volume and then get stuck at that volume level, and sometimes the problem just magically goes away even when all of these "conditions" are met!
Since the slider is the only UI element that's directly "connected" to the audio mixer and the problem stops once the audio mixer is not linked to the slider or is completely disabled, I'm thinking the culprit is the audio mixer itself. Which leads me to the question: why would an audio mixer take input from the keyboard where none has ever been specified??
I'm really hoping it's just some checkbox I missed.
I'm happy to post more of my code upon request for further clarification; I didn't want to bog down this post with walls of text.
Upvotes: 1
Views: 502
Reputation: 1694
Maybe in your input settings A and D are setted to set the value of the slider or for the axis and the axis will set the slider. Like Enter is setted to submit a button or so.
You can solve it by setting the navigation on your slider to None.
Upvotes: 2