Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.

Problem with audio commands and interactables in the same object.

edited October 2016 in Questions And Answers

the idea is that an object is tapped and there is a short summary, then, if the user says "tell me more", another narration goes into further detail for the component. Each code used the code from interactable where it had TargetFeedbackSound and audioSource. I tried it on the HoloLens and it clicked and spoke on different components. for each component, both selecting and saying "tell me more" played the same sound, the sound depending on wether the interactable or the speechcommand comes first in the interactable. how can I change the code below so I can have a second audioSource in the same object completely separate from the audioSource in the interactable script? Thanks.

using UnityEngine;
public class ComponentCommand : MonoBehaviour
{
    [Tooltip("Audio clip to play when interacting with this hologram.")]
    public AudioClip TargetCommandSound;
    private AudioSource audioSource2;
    private Material[] defaultMaterials;
    Vector3 originalPosition;

    // Use this for initialization
    void Start()
    {
        defaultMaterials = GetComponent<Renderer>().materials;

        // Add a BoxCollider if the interactible does not contain one.
        Collider collider = GetComponentInChildren<Collider>();
        if (collider == null)
        {
            gameObject.AddComponent<BoxCollider>();
        }
        EnableAudioHapticFeedback();
    }
    private void EnableAudioHapticFeedback()
    {
        // If this hologram has an audio clip, add an AudioSource with this clip.
        if (TargetCommandSound != null)
        {
            audioSource2 = GetComponent<AudioSource>();
            if (audioSource2 == null)
            {
                audioSource2 = gameObject.AddComponent<AudioSource>();
            }

            audioSource2.clip = TargetCommandSound;
            audioSource2.playOnAwake = false;
            audioSource2.spatialBlend = 1;
            audioSource2.dopplerLevel = 0;
        }
    }

    // Called by SpeechManager when the user says the "Tell me More" command
    void MoreInfo()
    {
        for (int i = 0; i < defaultMaterials.Length; i++)
        {
            defaultMaterials[i].SetFloat("_Highlight", .5f);
        }

        // Play the audioSource feedback when asked about component.

        if (audioSource2 != null && !audioSource2.isPlaying)
        {
            audioSource2.Play();
        }

    }
}
Tagged:

Best Answer

Answers

  • utekaiutekai ✭✭✭

    One way is to create references to the script for numerous audio sources, and then choose and play the one you want by selecting that reference you want.

    In this case you'd just have a single script for tell me more, and your script will have to determine which object is selected or that you're gazing at.

    Create public fields for each audio source in the script, and then in the UnityEditor, link that audio source to one of the fields in the script. Choose the right AudioSource based on the object you're gazing at.

    So you won't be using GetComponent, you'll use Unity wiring and the public AudioSource fields.

    Another way is to attach AudioSources to each object and send a message to the object to play it's audio source when you're gazing at it and have stated 'tell me more'.

  • @utekai said:
    One way is to create references to the script for numerous audio sources, and then choose and play the one you want by selecting that reference you want.

    In this case you'd just have a single script for tell me more, and your script will have to determine which object is selected or that you're gazing at.

    Create public fields for each audio source in the script, and then in the UnityEditor, link that audio source to one of the fields in the script. Choose the right AudioSource based on the object you're gazing at.

    So you won't be using GetComponent, you'll use Unity wiring and the public AudioSource fields.

    Another way is to attach AudioSources to each object and send a message to the object to play it's audio source when you're gazing at it and have stated 'tell me more'.

    I'm trying to do the second option, but each object has one sound for tapping and another for "tell me more" I tried an approach that creates multiple audioSources, but I don't know where to implement the rest of the code. any thoughts. the code below was added to Void Start, but everything else is the same, what left do I have to do?

    AudioSource[] audios = GetComponents();
    audioSource = audios[0];
    audioSource2 = audios[1];

Sign In or Register to comment.