Author Topic: 3D positional audio with Viewport  (Read 27 times)

fluqe

  • Newbie
  • *
  • Posts: 3
    • View Profile
3D positional audio with Viewport
« on: February 19, 2021, 08:37:05 PM »
I would like to have 3D positional audio in my game. For example, if there is an event like a battle or construction taking place somewhere, the user could hear some sounds emanating from audio source(s) there. When zooming in or the camera is close to an audio source, the sound would be louder. At max distance the user won't hear it at all. This would add more life and immersion to the map.

In 2D flat map mode, 3D audio works great. I tested it by adding an audio source to the tank in Example 204. On the Audio Source, Spatial Blend set to 1 (full 3d) and Custom Rolloff was needed. I'm guessing it would work fine in Terrain mode as well.

However, with Viewport mode (e.g. Example 201), there are some issues. Zooming in/out vertically doesn't affect the volume at all, but moving left or right reduces it very drastically. If you imagine the actual sound existing in 3d space, ideally the sound should be like a sphere around the object, but in Viewport mode it's like a column going up. Also the distance to the object as registered by the audio source is inconsistent when moving around. I guess this is related to the distance calculation and how the viewport's built-in camera works.

Are there any tips for getting 3D audio to work with Viewport? Perhaps by moving the Audio Listener from the Main Camera to the MapperCam? If it isn't feasible, I will just use 2D flat map, although it would be great to use Viewport since I really like the built-in features like clouds and horizontal wrap :)

I'm sorry if this has been asked about or addressed somewhere else, I searched but couldn't find anything.

EDIT: Also maybe this should have been a feature request, since I can get the functionality I want without the Viewport anyway :)
« Last Edit: Today at 04:52:04 AM by fluqe »

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 6630
    • View Profile
Re: 3D positional audio with Viewport
« Reply #1 on: Today at 08:29:37 AM »
Is the unit producing the sound attached to the viewport or to the 2D map?

In the viewport mode, zooming in/out is just an illusion as it's the mapper cam (not the main camera) which moves to/from the 2D map and the scales of the units on the viewport are adjusted accordingly. But in world space, the distance of the unit from the main camera doesn't change. It changes when you pan around. In this case, yes, there's an actual distance change from the 3D unity to the main camera as it moves off the screen.

Your pointer is correct - adding the AudioListener to the MapperCam AND the audiosource of the effects to invisible markers to the 2D map on the same position than the corresponding units. It's a workaround of course, but in that case the distance used by the AudioListener will be correct at all times. You could add an "Audio" gameobject children to your unit prefab and attach a script to this "audio" gameobject (which will be emitting the effect sound) so its position is updated automatically to match the 2D map, instead of the viewport.

For example, the MapPopulation demo scene uses a tank prefab that you can drop and move around. We can edit this prefab and add this "2D Auto Marker" game object. It's an empty gameobject with a simple script:



This is the script:

Code
using UnityEngine;
using WorldMapStrategyKit;

public class Auto2DMarker : MonoBehaviour
{
    GameObjectAnimator goa;

    void Start()
    {
        goa = GetComponentInParent<GameObjectAnimator>();
        goa.OnMove += Goa_OnMove;
    }

    private void Goa_OnMove(GameObjectAnimator anim) {
        transform.position = WMSK.instance.transform.TransformPoint(anim.currentMap2DLocation);
    }

}

The script gets a reference to the GameObjectAnimator component which is added by WMSK to all units added to the map.
Then, we listed to the OnMove event of the GameObjectAnimator which tells us the unit has moved so we move our position as well to the correct position in the 2D map (the 2D map is always the WMSK instance itself, the viewport is just another gameobject).

Now, you should have an invisible gameobject linked to your unit which is positioned at the correct location to be used with AudioSource componet. Attach this AudioSource to this children gameobject and use it to play the effect sounds.
You will also need to move the AudioListener to the MapperCam from the Main Camera.

fluqe

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: 3D positional audio with Viewport
« Reply #2 on: Today at 09:29:05 AM »
Thanks for the detailed explanation & solution! In the example where it wasn't working optimally, the unit was attached to the Viewport. I see what to do now, this is perfect :)