Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Ambient Emitter inside a BoxCollider - Rotation not recognized

Discussion in 'Scripting' started by Alexapo, Jun 4, 2021.

  1. Alexapo

    Alexapo

    Joined:
    Apr 6, 2021
    Posts:
    10
    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:

    Rotation - 000 - In game.png

    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:

    Rotation - 02200 - In game - jpg.jpg

    (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).

    Here is the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Ww_AmbientArea : MonoBehaviour
    6. {
    7.  
    8.     // All our physical needs
    9.     public bool Enable_Debug = false;
    10.     public GameObject AudioEmitter;
    11.     public GameObject Character;
    12.     public AkAudioListener Listener;
    13.     private BoxCollider AmbientCollider;
    14.     private Vector3 ListenerPosition;
    15.  
    16.     //All our calculation needs
    17.     private Vector3 ColliderSizeMax;
    18.     private Vector3 ColliderSizeMin;
    19.     private float emitterX;
    20.     private float emitterY;
    21.     private float emitterZ;
    22.  
    23.     public bool IsInArea = false;
    24.    
    25.  
    26.     // Start is called before the first frame update
    27.     void Start()
    28.     {
    29.         // DDefine that the collider we want to use is the one on this game object.
    30.         AmbientCollider = GetComponent<BoxCollider>();
    31.  
    32.         //Define the colliders size, scale and set it's border locations.
    33.         ColliderSizeMax = AmbientCollider.transform.position;
    34.         ColliderSizeMin = AmbientCollider.transform.position;
    35.         ColliderSizeMax += (transform.localScale / 2);
    36.         ColliderSizeMin -= (transform.localScale / 2);
    37.     }
    38.  
    39.     // Update is called once per frame
    40.     void Update()
    41.     {
    42.         ListenerPosition = Character.transform.position;
    43.  
    44.         if (ListenerPosition.x > ColliderSizeMin.x && ListenerPosition.x < ColliderSizeMax.x) { emitterX = ListenerPosition.x; }
    45.  
    46.         if (ListenerPosition.x < ColliderSizeMin.x) { emitterX = ColliderSizeMin.x; }
    47.  
    48.         if (ListenerPosition.x > ColliderSizeMax.x) { emitterX = ColliderSizeMax.x; }
    49.  
    50.         if (ListenerPosition.y > ColliderSizeMin.y && ListenerPosition.y < ColliderSizeMax.y) { emitterY = ListenerPosition.y; }
    51.  
    52.         if (ListenerPosition.y < ColliderSizeMin.y) { emitterY = ColliderSizeMin.y; }
    53.  
    54.         if (ListenerPosition.y > ColliderSizeMax.y) { emitterY = ColliderSizeMax.y; }
    55.  
    56.         if (ListenerPosition.z > ColliderSizeMin.z && ListenerPosition.z < ColliderSizeMax.z) { emitterZ = ListenerPosition.z; }
    57.  
    58.         if (ListenerPosition.z < ColliderSizeMin.z) { emitterZ = ColliderSizeMin.z; }
    59.        
    60.         if (ListenerPosition.z > ColliderSizeMax.z) { emitterZ = ColliderSizeMax.z; }
    61.  
    62.         if (!IsInArea) { AudioEmitter.transform.position = new Vector3(emitterX, emitterY, emitterZ); }
    63.     }
    64.  
    65.     private void OnTriggerStay(Collider other)
    66.     {
    67.         // If our player enters the collider and stays - check if IsInArea is false and set it to true.
    68.         // Set the emitters position to be the same as our listener position
    69.         if (other.tag != "Player") { return; }
    70.         if (!IsInArea) { IsInArea = true; }
    71.         AudioEmitter.transform.position = ListenerPosition;
    72.     }
    73.  
    74.     private void OnTriggerExit(Collider other)
    75.     {
    76.         // If we leave the collider - make sure that IsInArea is no longer true.
    77.         if (other.tag != "Player") { return; }
    78.          IsInArea = false;
    79.     }
    80. }
    81.  
    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#.
     
  2. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
    So yeah you would need to transform your collider size variables with the rotation, but could you not perhaps instead use Collider.ClosestPoint for an easier solution?
     
  3. Alexapo

    Alexapo

    Joined:
    Apr 6, 2021
    Posts:
    10
    That could very well be! I would need to do a bit more research on what it does first though, in order to understand how to use it. This code is bascially from a tutorial and since I am very new to programming, it is hard for me to approach a problem like this in another way than presented in the tutorial. Is it very difficult to transform the collider size variables with the rotation in the way it is set up right now?
     
  4. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
    Well, there is another issue in that the code seems to assume the collider size is set to 1 and the size is only set by scaling the transform, so you have to look out for that as well.

    A quick fix could be something like this (I don't have time to test it right now, sorry).

    Code (CSharp):
    1.         //Define the colliders size, scale and set it's border locations.
    2.         ColliderSizeMax = AmbientCollider.transform.position;
    3.         ColliderSizeMin = AmbientCollider.transform.position;
    4.         ColliderSizeMax += (transform.localScale / 2);
    5.         ColliderSizeMin -= (transform.localScale / 2);
    6.         // Rotate max and min points
    7.         ColliderSizeMax = transform.rotation * ColliderSizeMax;
    8.         ColliderSizeMin -= transform.rotation * ColliderSizeMin;
    9.  
     
  5. Alexapo

    Alexapo

    Joined:
    Apr 6, 2021
    Posts:
    10
    Hmmmm. It did not work for me at least. But thanks anyway! I will keep on researching :)
     
  6. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
    Should rotate around zero, so this is likely more correct, still not tested though.

    Code (CSharp):
    1.         //Define the colliders size, scale and set it's border locations.
    2.         ColliderSizeMax = (transform.localScale / 2);
    3.         ColliderSizeMin = -(transform.localScale / 2);
    4.         // Rotate max and min points
    5.         ColliderSizeMax = transform.rotation * ColliderSizeMax;
    6.         ColliderSizeMin = transform.rotation * ColliderSizeMin;
    7.         ColliderSizeMax += transform.position;
    8.         ColliderSizeMin += transform.position;
    9.