Setting parent of item sound effects

Cheo

Active member
Hello, when firing a gun the instantiated SharedAudioSource game objects are by default children of the Items object under the root object. But I couldn't find an option to set another parent for audio sources, I'd like to give gunshot sound effects parents near the fire point, am I missing something or do wo need to add some script lines ? One last thing, when reloading it seems that the audio source attached to the weapon is used for playing the reloading sound effect, I don't know why that effect differs from the rest.
 
You can create a new Audio Config and specify if the Audio Source should be reused or not.
 
That's not what I'm asking at all - I'd like to know if it is possible to set a parent for the instantiated audio sources out of the box, a bit like how we would assign a fire point for muzzle flashes. I haven't seen anything like that, I'll see if I can add a custom option for that.
 
Done - the Item Effect version of PlayAudioClip script only needed a few lines added.

C#:
/// ---------------------------------------------
/// Ultimate Character Controller
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------

namespace Opsive.UltimateCharacterController.Items.Actions.Effect
{
    using Opsive.Shared.Audio;
    using System;
    using UnityEngine;
    
    /// <summary>
    /// Plays an AudioClip when the effect starts.
    /// </summary>
    [Serializable]
    public class PlayAudioClip : ItemEffect
    {
        [Tooltip("A set of AudioClips that can be played when the effect is started.")]
        [SerializeField] protected AudioClipSet m_AudioClipSet = new AudioClipSet();

        public AudioClipSet AudioClipSet { get { return m_AudioClipSet; } set { m_AudioClipSet = value; } }
        [SerializeField] protected GameObject m_PlayGameObject;

        public GameObject PlayGameObject { get { return m_PlayGameObject; } set { m_PlayGameObject = value; } }

        /// <summary>
        /// Can the effect be started?
        /// </summary>
        /// <returns>True if the effect can be started.</returns>
        public override bool CanInvokeEffect()
        {
            return true;
        }

        /// <summary>
        /// Get the game object on which to play the audio clip.
        /// </summary>
        /// <returns>The game object on which to play the audio.</returns>
        protected virtual GameObject GetPlayGameObject()
        {
            if(m_PlayGameObject != null)
            {
                return m_PlayGameObject;
            }
            else
            {
                return m_CharacterItemAction.GameObject;
            }
        }

        /// <summary>
        /// The effect has been started.
        /// </summary>
        protected override void InvokeEffectInternal()
        {
            base.InvokeEffectInternal();

            var playGameObject = GetPlayGameObject();
            var audioSource = m_AudioClipSet.PlayAudioClip(playGameObject).AudioSource;
        }
    }
}

Could this be added in the next update ? This is a small detail that may add a bit to immersion.
 
Ah, thanks for posting your solution.

GetPlayGameObject is virtual so you can subclass PlayAudioClip and add a virtual method. This will give you the flexibility in being able to extend it and not conflict with any controller updates.
 
Top