Update 20181027
I’ve been working on a volume controller and options menu to interface with it. I finally worked out the issue with my SFX volume control was we were using the static AudioSource.PlayClipAtPoint method which seemed to ignore any input I gave on the volume parameter.
There are several posts online and some here on the GameDev.TV forum where it appears to work for some but I wasn’t haven’t luck. Rather than persist with banging my head against the wall I decided to replace the AudioSource.PlayClipAtPoint calls with audioSource.PlayOneShot(AudioClip myClip) calls. Now appears to be working and I can always revert down the track.
Here’s what the start and options menus are now looking like.
The Silly SFX option is a toggle/checkbox. I’m toying with the idea of replacing existing sound effects with voice recording of bang or pew. Here are some samples for those bored enough to be interested.
I’m using Panels on start menu canvas to display the start and options menus. When selecting Options, the start menu gets disabled and options enabled. Reverse when selecting main menu. I haven’t done anything for help section at this stage. Thinking of launching web browser / new tab to a web page with a game guide as seems more practical than creating a help scene in Unity.
Here’s the current state of my SoundManager script that sets the SFX to play.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoundManager : MonoBehaviour {
public static SoundManager Instance { get; private set; }
[SerializeField] AudioClip playerShotSFX, playerDeadSFX, enemyDeadSFX, enemyShotSFX, buttonClickSFX;
[SerializeField] AudioSource audioSource;
[SerializeField] [Range(0, 1)] float sFXVolume = 0.5f;
[SerializeField] [Range(0, 1)] float playerShotVolume = 1f;
[SerializeField] [Range(0, 1)] float playerDeadVolume = 1f;
[SerializeField] [Range(0, 1)] float enemyShotVolume = 1f;
[SerializeField] [Range(0, 1)] float enemyDeadVolume = 1f;
[SerializeField] [Range(0, 1)] float buttonClickVolume = 1f;
private void Awake()
{
SetUpSingleton();
SetSFXVolume(sFXVolume);
}
private void SetUpSingleton()
{
// First we check if there are any other instances conflicting
if (Instance != null)
{
// If that is the case, we destroy other instances
Destroy(gameObject);
}
else
{
// Here we save our singleton instance
Instance = this;
// Furthermore we make sure that we don't destroy between scenes (this is optional)
DontDestroyOnLoad(gameObject);
}
}
public void TriggerPlayerShotSFX()
{
audioSource.PlayOneShot(playerShotSFX, (sFXVolume * playerShotVolume));
}
public void TriggerPlayerDeadSFX()
{
audioSource.PlayOneShot(playerDeadSFX, (sFXVolume * playerDeadVolume));
}
public void TriggerEnemyShotSFX()
{
audioSource.PlayOneShot(enemyShotSFX, (sFXVolume * enemyShotVolume));
}
public void TriggerEnemyDeadSFX()
{
audioSource.PlayOneShot(enemyDeadSFX, (sFXVolume * enemyShotVolume));
}
public void TriggerButtonClickSFX()
{
audioSource.PlayOneShot(enemyDeadSFX, (sFXVolume * enemyShotVolume));
}
public float GetSFXVolume()
{
return sFXVolume;
}
public void SetSFXVolume(float newVolume)
{
sFXVolume = newVolume;
audioSource.volume = sFXVolume;
}
}
And here’s the volume controller which is attached to my volume sliders in the options menu.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class VolumeController : MonoBehaviour {
[SerializeField] Slider sFXVolumeSlider;
[SerializeField] Slider musicVolumeSlider;
public void SetSFXVolume()
{
SoundManager.Instance.SetSFXVolume(sFXVolumeSlider.value);
}
public void GetSFXVolume()
{
sFXVolumeSlider.value = SoundManager.Instance.GetSFXVolume();
}
public void SetMusicVolume()
{
MusicPlayer.Instance.SetMusicVolume(musicVolumeSlider.value);
}
public void GetMusicVolume()
{
musicVolumeSlider.value = MusicPlayer.Instance.GetMusicVolume();
}
}
I’m next wanting to tackle the problem of game states. I’m picturing 4 potential states.
-
InMenu - Indicating player is in start menu, between levels or game over menu.
-
InEvent - Indicating player is in a scripted event/animation/cut scene. Something I hope to implement. Not a big moving event. More just a game like scene with scripted player/enemy movement and dialogue windows appearing to deliver story. Not sure how but we’ll get there… Eventually.
-
InGame - Indicating player is in an active level and playing, e.g. can shoot, move, etc.
-
IsPaused - Indicating game is paused. May cross over with InMenu.
More than likely need to break this problem into smaller bits to implement. See how I go.
If you have any advice or feedback, feel free to post here in the thread.
Take care,
HorusOfOz