smallarrows.GIF
titlebar_image

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: audio files
(SteamVR included some sounds as well.)


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 Min Distance to a low number such as .5 to start.
     Change the Max Distance to a low number such as 1 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
Detached Hand does not work in the simulator, you need VR.
(You can always use Hand Hover for the simulator.)

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Sound when an object Collides
This does not work on animations from Maya such as a bouncing ball. It works great with throwable objects. It may work with Unity animated objects.

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();
    }
}


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Click Events Helper Script to add Sound
Download these script files: door_scripts.zip

Place the ClickEventsHelper.cs script into your Assets folder

Select the model in the Hierarchy

     Add Component->Physics->Box Collider    (Any collider will work.)

Drag the ClickEventsHelper.cs script onto your model.

In the Inspector, select the + to add an event to the Click Events Helper script
     Add the model to the box that reads None (Object)
    Change No Function to AudioSource->Play ()

unity_open_door_audio

** The SteamVR Interactable Hover Events script does not work with Click Events Helper script.


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

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

© 1988-2024 36 years