How To Tie Audio To GameObjects, Like The Player

Discuss modding questions and implementation details.
Post Reply
l3lessed
Posts: 1400
Joined: Mon Aug 12, 2019 4:32 pm
Contact:

How To Tie Audio To GameObjects, Like The Player

Post by l3lessed »

Hey Everyone, I thought I would release a quick guide/post on an easy way to setup and play classic audio sounds I recently used in my shield module. This is for those who have not done it yet, or just want to see a possibly different way of doing it. If you use this, a quick shout out and reference to my work would be appreciated in your mod documentation. :D

So, in order to do it, there are three simple steps to setting up the script. It really takes less than five minutes, if you already know all the objects you are going to call and use.

1. Setup the DaggerfallAudioSource as a general object property in your script: this will be used to load the audio control object and store it for attaching to other game objects. Drop it into your script properties area, like so.

Code: Select all

        //sets up dfAudioSource object to attach objects to and play sound from said object.
        DaggerfallAudioSource dfAudioSource;
2. Find the object you want to attach the loaded DaggerfallAudioSource object to and set it up for referencing. Here, I'm using the player as the object to attach it to, since it is for my shield module. You can technically attach this to any registered game object.

Here, I setup the gameobject I'm going to attach it to in the general script properties area to ensure it can be accessed throughout the script object it is embedded in; this is fine because I'm attaching it to the player, which never changes gets removed during playing. If you are attaching it to dynamic objects, ensure to set it up wherever your setup the game object your creating and destroying.

Code: Select all

        //used for attaching audio control object to and playing audio from.
        GameObject player;
then in the Start area load the player object and then load and attach the DaggerfallAudioSource object to said player object.

Code: Select all

        void Start()
        {
            //finds the player and assigns them to the player objects.
            player = GameObject.FindGameObjectWithTag("Player");
            //finds daggerfall audio source object, loads it, and then adds it to the player object, so it knows where the sound source is from.
            dfAudioSource = player.AddComponent<DaggerfallAudioSource>();
Now, all we have to do is access the dfAudioSource object anywhere in the script to tell it what ti do audio wise, if its play sounds, set clips, or set other properties around the objects audio sounds you emit from it.

In my shield module, I needed sound feedback for raising shields and being hit. All I had to do call the object on a if-then check and tell it to play the equip clothing sound built into the engine, like so:

Code: Select all

                //plays cloth equipping sound with a reduced sound level to simulate equipment rustling.
                dfAudioSource.PlayOneShot(417,1,.3f);
The int 417 is merely a reference to a predefined table of default daggerfall sound clips. You can look up the table set by using the "SoundsClips" enum reference anywhere in a script.

Here is one more, more dynamic use of my dfAudioSource. On thing I found out quickly was using the same parry sound over and over for the hit sound made it feel very robotic and out dated. Nothing in reality sounds 100% alike. So, in order to resolve this, there was an easy solution. Combine the dfRand object with the dfAudioSource object to randomly select from the parry SoundClip list range, like so:

Code: Select all

                        dfAudioSource.PlayOneShot(DFRandom.random_range_inclusive(428, 436));
This is triggered one time, every time the player is hit, by a simple bool isHit switch.

One last note, you can adjust the 3D audio simulation level unity has built into it by using the second input parameter "spatial blend". You can adjust the volume with the third input parameter. By combining and playing with these two inputs and numerous sound clips, you can create dynamic, unique sounds that will not be noticeable copies of each other, even though they are.
My Daggerfall Mod Github: l3lessed DFU Mod Github

My Beth Mods: l3lessed Nexus Page

Daggerfall Unity mods: Combat Overhaul Mod

Enjoy the free work I'm doing? Consider lending your support.

User avatar
BadLuckBurt
Posts: 948
Joined: Sun Nov 05, 2017 8:30 pm

Re: How To Tie Audio To GameObjects, Like The Player

Post by BadLuckBurt »

Nice, just to add that to get the player object, you can also use the GameManager instance:

When you've added

Code: Select all

using DaggerfallWorkshop.Game;
Just do

Code: Select all

GameObject player = GameManager.Instance.PlayerObject;
If you don't use the using stuff it'd be

Code: Select all

GameObject player = DaggerfallWorkshop.Game.GameManager.Instance.PlayerObject;
The Find method will work fine too
DFU on UESP: https://en.uesp.net/w/index.php?title=T ... fall_Unity
DFU Nexus Mods: https://www.nexusmods.com/daggerfallunity
My github repositories with mostly DFU related stuff: https://github.com/BadLuckBurt

.

l3lessed
Posts: 1400
Joined: Mon Aug 12, 2019 4:32 pm
Contact:

Re: How To Tie Audio To GameObjects, Like The Player

Post by l3lessed »

Thanks for pointing that out. I imagine that is a faster reference than using the find methid, which would matter if you were spawning the objects dynamically.
My Daggerfall Mod Github: l3lessed DFU Mod Github

My Beth Mods: l3lessed Nexus Page

Daggerfall Unity mods: Combat Overhaul Mod

Enjoy the free work I'm doing? Consider lending your support.

User avatar
Ralzar
Posts: 2211
Joined: Mon Oct 07, 2019 4:11 pm
Location: Norway

Re: How To Tie Audio To GameObjects, Like The Player

Post by Ralzar »

I recently had to figure out something similar to attach light and sound to my campfire. These two methods can be easily rewritten for any objects.

Attach light:

Code: Select all

private static GameObject AddLight(DaggerfallUnity dfUnity, GameObject obj, Transform parent)
{
    Vector3 position = FirePosition;
    GameObject go = GameObjectHelper.InstantiatePrefab(dfUnity.Option_DungeonLightPrefab.gameObject, string.Empty, parent, position);
    Light light = go.GetComponent<Light>();
    if (light != null)
    {
	light.range = 15;
    }

    return go;
}

Attach sound:

Code: Select all

private static void AddTorchAudioSource(GameObject go)
{
    DaggerfallAudioSource c = go.AddComponent<DaggerfallAudioSource>();
    c.AudioSource.dopplerLevel = 0;
    c.AudioSource.rolloffMode = AudioRolloffMode.Linear;
    c.AudioSource.maxDistance = 5f;
    c.AudioSource.volume = 0.7f;
    c.SetSound(SoundClips.Burning, AudioPresets.LoopIfPlayerNear);
}

l3lessed
Posts: 1400
Joined: Mon Aug 12, 2019 4:32 pm
Contact:

Re: How To Tie Audio To GameObjects, Like The Player

Post by l3lessed »

Thanks for sharing that. I like the audio setup for the torch. It shows the clear difference between how PlayOneShot works to instantly play a quick clip based on a code trigger vs how SetSound works for tying sound clips to world objects to trigger based on the input parameters programmed.

Nice to see how to setup lights, since I haven't even bothered or needed to look into that.
My Daggerfall Mod Github: l3lessed DFU Mod Github

My Beth Mods: l3lessed Nexus Page

Daggerfall Unity mods: Combat Overhaul Mod

Enjoy the free work I'm doing? Consider lending your support.

User avatar
Ralzar
Posts: 2211
Joined: Mon Oct 07, 2019 4:11 pm
Location: Norway

Re: How To Tie Audio To GameObjects, Like The Player

Post by Ralzar »

Yeah, it turned out to be handy just yesterday when I realized the horse I was placing in my Realistic Wagon mod was a mute :D

Note the last lines change to PlayRandomlyIfPlayerNear.

Code: Select all

private static void AddHorseAudioSource(GameObject go)
{
    DaggerfallAudioSource c = go.AddComponent<DaggerfallAudioSource>();
    c.AudioSource.dopplerLevel = 0;
    c.AudioSource.rolloffMode = AudioRolloffMode.Linear;
    c.AudioSource.maxDistance = 5f;
    c.AudioSource.volume = 0.7f;
    c.SetSound(SoundClips.AnimalHorse, AudioPresets.PlayRandomlyIfPlayerNear);
}

Post Reply