Unity Build ignores audio spread
Im using unity 2020.1.13f1 and I'm building to WedGL. Inside of unity my audio behaves how i want. I have turned on 3D audio but set the spread to 180. This way i do not have any stereo effect walking past the source but i do get the audio to get louder and softer walking towards and away from it. Once I export though all of those settings seem to go out the window and i get a really harsh pan from left to right. This is a 2D game and i do have the audios on the same Z position as the camera. Why does it sound one way in unity then ignores my hard work in the build.
Answer by Da_Elf · Aug 31, 2021 at 03:22 PM
Ok. No answers came so since I dont know how to do it in the audio source I thought i might as well do it in code. It might be sloppy but hey. It gets the job done (would like to be able to hide the gizmos when not selected in the scene)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioDistance : MonoBehaviour
{
AudioSource AS;
float originalVolume;
public float maxDistance;
public float minDistance;
void Awake()
{
AS = GetComponent<AudioSource>();
originalVolume = AS.volume;
AS.volume = 0f;
}
void Update()
{
GameObject player = GameObject.Find(GM.gm.userName);
if (player != null)
{
float percentBetween = 0f;
float shiftMax = Mathf.Abs(maxDistance-minDistance);
float playerDistance = Vector2.Distance(player.transform.position,transform.position);
if (playerDistance > maxDistance){percentBetween = 0f;}
else if (playerDistance < minDistance){percentBetween = 1f;}
else
{
percentBetween =
((shiftMax - (playerDistance-minDistance))
/
shiftMax);
}
AS.volume = Mathf.Abs(originalVolume * percentBetween);
}
}
void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position,maxDistance);
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(transform.position,minDistance);
}
}
Your answer
Follow this Question
Related Questions
Could someone help me with adding Audio to this code? (Is for a 2D game platformer) 0 Answers
audio import errors in webGL 1 Answer
Pick Up Sound 0 Answers
Trying to use derive pitch and db from realtime microphone input 0 Answers
Trying to make a splitscreen game, having issues with the AudioSource and distance between objects. 0 Answers