Ambient Emitter inside a BoxCollider - Rotation not recognized
So, I am trying to create a system for playing an ambience SFX. What I have done is create a box collider for a spesific area where I want a spesific ambience sound to play and created an Audio Emitter (sphere collider) that has the same location as the player and moves around with him when he is inside the collider. What I want is for the sound to stop when the player exits the collider and to start when he enters the collider - pretty simple stuff. When the player is outside the collider, the audio emitter inside the collider will keep track of the players location and move within the colliders borders so that when the player enter the collider, the audio emitter will basically be "attached" to the players location.
Something like this:
(Player outside the box collider)
However, the problem I am having is that this only works when the rotation of the collider is (0,0,0). When I rotate the collider, it seems that the code does not recognize the rotation of the collider and the audio emitter is still confined to the same borders of the collider that it had when its rotation was (0,0,0) . Like this:
(Player outside the collider when the rotation is (0,22,0)
This is obviously a problem, since many buildings and areas I want to give an ambience to does not always have a rotation of (0,0,0).
This is the code I have:
// All our physical needs
public bool Enable_Debug = false;
public GameObject AudioEmitter;
public GameObject Character;
public AkAudioListener Listener;
private BoxCollider AmbientCollider;
private Vector3 ListenerPosition;
//All our calculation needs
private Vector3 ColliderSizeMax;
private Vector3 ColliderSizeMin;
private float emitterX;
private float emitterY;
private float emitterZ;
public bool IsInArea = false;
// Start is called before the first frame update
void Start()
{
// DDefine that the collider we want to use is the one on this game object.
AmbientCollider = GetComponent<BoxCollider>();
//Define the colliders size, scale and set it's border locations.
ColliderSizeMax = AmbientCollider.transform.position;
ColliderSizeMin = AmbientCollider.transform.position;
ColliderSizeMax += (transform.localScale / 2);
ColliderSizeMin -= (transform.localScale / 2);
}
// Update is called once per frame
void Update()
{
ListenerPosition = Character.transform.position;
if (ListenerPosition.x > ColliderSizeMin.x && ListenerPosition.x < ColliderSizeMax.x) { emitterX = ListenerPosition.x; }
if (ListenerPosition.x < ColliderSizeMin.x) { emitterX = ColliderSizeMin.x; }
if (ListenerPosition.x > ColliderSizeMax.x) { emitterX = ColliderSizeMax.x; }
if (ListenerPosition.y > ColliderSizeMin.y && ListenerPosition.y < ColliderSizeMax.y) { emitterY = ListenerPosition.y; }
if (ListenerPosition.y < ColliderSizeMin.y) { emitterY = ColliderSizeMin.y; }
if (ListenerPosition.y > ColliderSizeMax.y) { emitterY = ColliderSizeMax.y; }
if (ListenerPosition.z > ColliderSizeMin.z && ListenerPosition.z < ColliderSizeMax.z) { emitterZ = ListenerPosition.z; }
if (ListenerPosition.z < ColliderSizeMin.z) { emitterZ = ColliderSizeMin.z; }
if (ListenerPosition.z > ColliderSizeMax.z) { emitterZ = ColliderSizeMax.z; }
if (!IsInArea) { AudioEmitter.transform.position = new Vector3(emitterX, emitterY, emitterZ); }
}
private void OnTriggerStay(Collider other)
{
// If our player enters the collider and stays - check if IsInArea is false and set it to true.
// Set the emitters position to be the same as our listener position
if (other.tag != "Player") { return; }
if (!IsInArea) { IsInArea = true; }
AudioEmitter.transform.position = ListenerPosition;
}
private void OnTriggerExit(Collider other)
{
// If we leave the collider - make sure that IsInArea is no longer true.
if (other.tag != "Player") { return; }
IsInArea = false;
}
Does anyone know how I can make it work even if I set the roation of the box collider to something other than (0,0,0). I am pretty new to programming and C#.