I want to make the gameplay music of my game more random, so every time the scene load, a different event with the music in it plays.
So far I being able to create some sort of script to create a list in which I can drag others gameObjects with the fmod emitter component inside and the music I choose.
this is the list in which i can drag the slaves gameObjects
So far the sound is working, every event is playing at the same time, my question is: is there a way to play on single event of this list every time the scene loads??
Hope you guys understand Im not a native english speaker.
The reason they are always triggering is that each event emitter has it’s Play EventUnity Integration | Game Components - Trigger Condition set to Object Start which will trigger the event when the object is created in the scene.
To stop this we will want to change the condition to None
Now, to start the random event we could use a random number to trigger a track from our list:
public List<FMODUnity.StudioEventEmitter> tracks = new List<FMODUnity.StudioEventEmitter>();
int trackToPlay = 0;
void Start()
{
trackToPlay = Random.Range(0, tracks.Count);
tracks[trackToPlay].Play();
}
private void OnDestroy()
{
tracks[trackToPlay].Stop();
}
This method also removes the need for gameMusic as the event emitters contain their own event instances for us.
This could also be done in FMOD Studio using parameters to control which audio track is played in an event, this would remove the need for any coding. An example can be found in the Example Project included in the FMOD Studio download: C:\Users\XXX\Documents\FMOD Studio\examples
how do I create probability for these list events???
is there a chance to add some type of function to stop playing the events already play until the list is complete??
thank you for your time replying this questions my good Connor I love you!!!
Could you elaborate on what you mean by this? If you a referring to the randomization then that comes from the Random.Range(0, tracks.Count) function which will return a number between 0 and the number of tracks in the list: Unity Docs | Random Range.
An option may be keeping track of the TrackToPlay numbers and if it is going to trigger the same number again you could just call Random.Range() again till you find a number that has not yet been used.
saying probability i mean like every time the scene loads with the event list in it, this events inside could have some type of percentage (%) to be more probably to sound the event with 33% rather than one event who have only 2% from the 100% all the events share (i say this because i make some music event i would like to play only a very few times in the game, like an easter egg or something like that
I would like to hear every event first rather than hear one event which already hear the last time scene load, don´t play an event which already play untill all other events in the list has been listened too…
i hope my grammar interfiere in the idea!!!
and one more question if i may:
in this script is there a way to create sometype of pause function on which the list pause the sounds when we call the pause method???
This will allow you to assign those easter egg files with lower percentage chance of playing.
Unfortunately, a multi-instrument won’t be able to remember which instruments it has already played from its list. However, with the code you could keep track of the trackToPlay value and make sure that it hasn’t already been called. I would suggest using a list that is saved during scene loading using DontDestroyOnLoad() (Unity Documentation - DontDestoryOnLoad()) then checking if the trackToPlay value is already in the list if it is then try find a new value. Else, the value is safe to play and can be used.