Showing posts with label Unity. Show all posts
Showing posts with label Unity. Show all posts

Wednesday, October 30, 2013

Using OSC to Control Unity - Simple Demo



I'll be doing a write up on this shortly.







Saturday, October 26, 2013

Unity Sound Example - Simple LFO Modulator

A quick demo that makes use of Mathf.sin(), Update() and a variable to modulate the pitch of an audio asset via an LFO.

In short, the process is:
• Find an in-game object that currently triggers a single sound
• Find the script that is associated with that object that triggers that sound
• Create a variable at the top of the code for indexing the LFO, e.g.:
var LFO_index : float = 0.0; 
• Find or create the update() { } function in the same script
• Add the following code to the update() function:
LFO_index = LFO_index + 0.1;
audio.pitch = 1.0 + (Mathf.Sin(LFO_index) * 0.2);
• In the above two lines of code, changing the value of "0.1" will adjust the speed of the LFO modulator
• In the above two lines of code, changing the value of "0.2" will adjust the depth of the LFO modulator
• In the above two lines of code, the audio.pitch attribute can be changed to something else (e.g. audio.volume etc).


Unity Sound Example - Contiguous File Playback from Event Trigger

This is a quick example showing how the same event trigger can play a different sound asset from an array in order - using a variable, modulo and an AudioClip[] array.

In short:
• Find an in-game object that currently triggers a single sound
• Find the script that is associated with that object that triggers that sound
• Add an AudioClip[] array variable to the top of the code e.g.:
var SoundFileNames : AudioClip[];
• Add a variable to the top of the code for storing the index of the sound file name array e.g.:
var SoundIndex : int = 0;
• Find the audio.Play() function within that script
• Add code above audio.play() that reads:
SoundIndex = (SoundIndex + 1) % SoundFileNames.length;
audio.clip = SoundFileNames[SoundIndex];
• Remember to inspect the object in Unity via the Inspector and populate the audio clip array with audio assets!


Friday, October 25, 2013

Unity Sound Example - Looping, Random Sound Files for Triggered Event

A quick demo that makes use of AudioClip[], audio.loop, audio.isPlaying, update(), and a true / false flag in order to play back a looping array of randomised sounds when an event trigger is activated.

In this example, a single sound source that loops is replaced with an array of sound assets that are randomly played back for every loop.

This example uses the unity 4.x demo "Angrybots". The standard autofire gunshot sound - which loops continuously when the fire button is held down - is replaced with an array of sounds (which could be slightly varied gunshot sounds) and change with every loop.

In short, the process for this particular example is:
• Find an in-game object that currently triggers a single sound that loops continuously
• Find the script that is associated with that object that triggers that sound
• Add an AudioClip[] array variable to the top of the code e.g.:
var RandomSoundNames : AudioClip[];
• Find the audio.play() function within that script
• Add code above audio.play() that reads:
audio.clip = RandomSoundNames[Random.Range(0, RandomSoundNames.length)];
audio.loop = false;

• Find or create the update() { } function in the same script
• Add the following code to the update() function:
if(autofire == true && audio.isPlaying == false) {
audio.clip = RandomSoundNames[Random.Range(0, RandomSoundNames.length)];
audio.play();
}

• Remember to inspect the object in Unity via the Inspector and update the random audio array with audio assets!


Wednesday, October 23, 2013

Unity Sound Example - Random Audio File Playback

A quick demo that makes use of AudioClip[] array, audio.clip and Random.Range() to randomise the audio asset file that is played back when an event is triggered.

In short, the process is:
• Find an in-game object that currently triggers a single sound
• Find the script that is associated with that object that triggers that sound
• Define a new variable at the start of that script to create an array of audio clips,  e.g. var RandomSounds: AudioClip[];
• Find a reference to audio.Play(); within the script
• Randomise the selection of the audio clip in the line before audio.Play(); e.g. audio.clip = RandomSounds[Random.Range(0,RandomSounds.length)];
• Save the script and run the game again


Unity Sound Example - Random Volume

A quick demo that makes use of audio.volume and Random.Range() to randomise the volume of a triggered sound in Unity.

In short, the process is:
• Find an in-game object that currently triggers a single sound
• Find the script that is associated with that object that triggers that sound
• Find a reference to audio.Play(); within the script
• Randomise the volume of the audio source in the line before audio.Play(); e.g. audio.volume = Random.Range(0.1, 1.0);
• Save the script and run the game again
• Note that the parameter of volume is an amplitude multiplier (e.g., 1.0 is 100% amplitude, 0.5 is 50% amplitude etc).



Unity Sound Example - Random Pitch

A quick demo that makes use of audio.pitch and Random.Range() to randomise the pitch of a triggered sound in Unity.

In short, the process is:
• Find an in-game object that currently triggers a single sound
• Find the script that is associated with that object that triggers that sound
• Find a reference to audio.Play(); within the script
• Randomise the pitch of the audio source in the line before audio.Play(); e.g. audio.pitch = Random.Range(0.5, 2.0);
• Save the script and run the game again
• Note that the parameter of pitch is a sound playback rate / ratio value (e.g. 1.0 is normal pitch, 0.5 is half pitch, 2.0 is an octave up etc).