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.

Playing audio clips at various gameobject points

I have 8 empty game objects around the camera and I want the same sounds to come from those points in the world.
How can I PlayClipAtPoint correctly to accomplish this.
This script is on my Audio Manager Empty game object. But i want the audio to come from Point1-Point8 transforms position.

`
using UnityEngine;
using System.Collections;

public class AudioManagerScript : MonoBehaviour
{

public Transform point1;
public Transform point2;
public Transform point3;
public Transform point4;
public Transform point5;
public Transform point6;
public Transform point7;
public Transform point8;
// Use this for initialization
public AudioClip clip1;
public AudioClip clip2;
public AudioClip clip3;
public AudioClip clip4;
public AudioClip clip5;
public AudioClip clip6;
public AudioClip clip7;
public AudioClip clip8;
public AudioClip clip9;
public AudioClip clip10;

public AudioSource audio;
void Start()
{
    InvokeRepeating("myFunc", 1.0f, 1.0f);
    audio = GetComponent<AudioSource>();
}

void myFunc()
{
    audio.PlayClipAtPoint(clip1,point1.transform.position,0.9f);
}

}

`

Best Answer

  • Accepted Answer

    Solved problem

    `using UnityEngine;
    using System.Collections;

    public class AudioManagerScript : MonoBehaviour
    {
    public Transform[] points;

    // Use this for initialization
    public AudioClip[] clips;
    
    //public new AudioSource audio;
    void Start()
    {
        // audio = GetComponent<AudioSource>();
        clips = Resources.LoadAll<AudioClip>("Audio");
    
        InvokeRepeating("myFunc", 1.0f, 1.0f);
    }
    
    void myFunc()
    {
    
        // audio.PlayOneShot(clip1, 0.9f);
        // PlayClipAtPoint(sources[0], point[0].transform.position,0.9f);
        //sources[0].Play();
    
        AudioClip chosenClip = clips[Random.Range(0, clips.Length)];
        Transform chosenPoint = points[Random.Range(0, points.Length)];
        AudioSource.PlayClipAtPoint(chosenClip, chosenPoint.position, 0.9f);
    }
    

    }

    `

Answers

  • What effect are you trying to achieve? If you just want some global surrounding sounds you can attach an audio component to the main camera.

  • I want to play 1 of 100 clips every second from the 8 empty game objects i have around the camera. the 100 clips are in the folder Resources/Audio and are named 1.mp3-100.mp3

  • So you are trying to achieve a spatial sound, but you need them all synced and playing together?

  • Accepted Answer

    Solved problem

    `using UnityEngine;
    using System.Collections;

    public class AudioManagerScript : MonoBehaviour
    {
    public Transform[] points;

    // Use this for initialization
    public AudioClip[] clips;
    
    //public new AudioSource audio;
    void Start()
    {
        // audio = GetComponent<AudioSource>();
        clips = Resources.LoadAll<AudioClip>("Audio");
    
        InvokeRepeating("myFunc", 1.0f, 1.0f);
    }
    
    void myFunc()
    {
    
        // audio.PlayOneShot(clip1, 0.9f);
        // PlayClipAtPoint(sources[0], point[0].transform.position,0.9f);
        //sources[0].Play();
    
        AudioClip chosenClip = clips[Random.Range(0, clips.Length)];
        Transform chosenPoint = points[Random.Range(0, points.Length)];
        AudioSource.PlayClipAtPoint(chosenClip, chosenPoint.position, 0.9f);
    }
    

    }

    `

Sign In or Register to comment.