smallarrows.GIF

156 syllabus      site index      3D artists      past student work        DMA

Unity Sound

Written by Alex Rickett

Save at least one sound file into your Assets folder

Basically any file format is OK (wav, aiff, mp3, mp4, ogg)
Free audio files: https://www.freesfx.co.uk/



Background Looping Sounds
Add an AudioSource to the object you want to make a sound.  (If you want it to be an ambient sound, like background music, you can create a new empty object and put it on that.)

Select the object to have sound in the
Hierarchy

Select Add Component->Audio Source
     Drag an drop a sound clip from the Assets tab onto the slot for AudioClip

Check the Loop
box, if you would like it to play more than once.



:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
3D Sound Based on Distance

Select your object that will play sounds, or create a GameObject->Create Empty

Select Add Component->Audio Source

     Drag an drop a sound clip from the Assets tab onto the slot for AudioClip
     Select Loop
if you like
     Select Spatial Blend 1 (3D)   This is the most important setting.
     Change the Max Distance to a low number such as 3 to start.



:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Sound when you touch an object
You need SteamVR installed for this

Select your object that will play sounds.

Make sure it has a collider.
Select the model in the Hierarchy
     Add Component->Physics->Box Collider or any other collider.

Select Add Component->Audio Source

     Drag an drop a sound clip from the Assets tab onto the slot for AudioClip
     Uncheck the Play on Awake
.


Select the Model in the Hierarchy
Use Add Component
     Search for inter
     Select Interactable Hover Events
Hit the + under On Hand Hover Begin () or one of the other selections, in the Inspector

Drag and drop Model from the Hierarchy onto None (Object) in the Inspector
Click the drop down menu reading No function and choose Audio Source->Play()
unity_steam_audio_event

With a VR controller, On Attached To Hand () uses the trigger by default



:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Sound when an object Collides
Select an object with colliders
Select
Add Component->Audio Source
     Drag an drop a sound clip from the Assets tab onto the slot for AudioClip

Select the object in the Hierarchy
Select the Add Component
 button
     Scroll down to New script
          Type the name: ImpactSound
          Double click the C# script in your Assets folder

Replace entire script with this script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ImpactSound : MonoBehaviour
{
    AudioSource source;
    void Start()
    {
        source = GetComponent<AudioSource>();
        source.volume = 0.0f;
    }

    private void OnCollisionEnter(Collision collision)
    {
        source.volume = Random.Range(0.5f, 1f);
        source.Play();
    }
}




:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
If you do not hear your sound
PC: Select the audio icon in the lower right side of the screen.
vive_falcon_audio


Make sure the headset is active.
Headphones are connected to the headset
The sound output should be Vive


smallarrows.GIF