[Question Using SoundFX]

Hello All,

I am having a problem with sound in Unity - I always have. So hopefully this question will help clear them up once and for all.

I am using this script to play a sound for a dog caught in a trap - unfortunately I don’t have a closed trap model. Anyway when the game starts I am using this script:

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

public class wolfSound : MonoBehaviour {

    public AudioSource audio;
    public AudioClip clip;

    //is the wold still in the trap or free
    bool isFree;


    // Use this for initialization
    void Start () {
        isFree = false;
        audio = GetComponent<AudioSource>();

        if (!isFree){
            print("not free");
            audio.PlayOneShot(clip, .1f);
        }
    }
   
    // Update is called once per frame
    void Update () {
    }//end update

}//end Class

Now this plays the sound at the beginning of the game, expectedly, so, except that in AudioSource component I have the Max Distance set for the sound at 15 metres and the 3rd person control is a lot further than that, so, it shouldn’t be heard. It is very loud and then when I do get within that 15 metre distance the sound can barely be heard. Also in the AudioSource component I have the sound looped - I realise this is probably the worst way to do it was the only way I could get the sound to function without sound contantly firing every game loop.

So I would be very appreciative if someone could tell me what I am doing wrong and anyway to rectify these problems.

Thankyou and Regards.

You’re using 0.1 as the volume scale.

Try a value of 1.

hahaha … yes, well spotted, that does make a difference when I get closer to the animal when it’s looping - within the max distance :p.

But I now remember why I set to to “0.1f”, I was trying to stop the sound from being played at the start of the game - it’s louder than anything else at the beginning of the game, and I mean loud, even though I am outside the max distance - it shouldn’t be playing at all - I can’t stop it from playing at the start even though “Play on Awake” is not ticked.

So, is there anyway I can stop this?

Remove the line of code calls the PlayOneShot() function - I don’t think you need this for what you’re trying to do.

Assign your audio clip to the audio source in the inspector, set max distance to 15 and check “PlayOnAwake” and “Loop”. Make sure to set the “Spatial Blend” to 3D (a value of 1) so that the clip inst played in 2D.

The Audiosource should play on start and you should only be able to hear it within the range of 15m.

Once the poor dog is free from the trap, simply call audio.Stop() to stop the audioSource.

1 Like

Thanks for that - that makes everything work perfectly :sunglasses:.

And thanks for the tip about stopping the sound as well - the poor dog is very happy now :smile:.

Thankyou and Regards.