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

Disable sound inside building.

Discussion in 'Editor & General Support' started by mcgamerx19, Aug 21, 2014.

  1. mcgamerx19

    mcgamerx19

    Joined:
    Jul 23, 2014
    Posts:
    19
    Is I want to have a wind sound outside a building. But then when I go inside the building model to plays a different sound. How would I go about doing that? Thanks.
     
  2. MakeCodeNow

    MakeCodeNow

    Joined:
    Feb 14, 2014
    Posts:
    1,246
    You'll want to setup a trigger w/ a custom script for the building. When the player enters the building you stop the outside sound and play the inside sound and vice versa.

    Alternatively, you can check out my product SECTR AUDIO which does stuff like this (and much more!) out of the box.
     
  3. mcgamerx19

    mcgamerx19

    Joined:
    Jul 23, 2014
    Posts:
    19
    sorry but i don't feel like paying 50$ for this. Anyone have any other solutions?
     
  4. MakeCodeNow

    MakeCodeNow

    Joined:
    Feb 14, 2014
    Posts:
    1,246
    My first para is the diy solution. No purchase required, but takes some coding.
     
  5. mcgamerx19

    mcgamerx19

    Joined:
    Jul 23, 2014
    Posts:
    19
    Ok I got it. I'm new to learning code and just made my first piece!!! (Feels so good)
    Code (JavaScript):
    1. #pragma strict
    2. private var enter : boolean;
    3. var sound : GameObject;
    4. var shot : AudioClip;
    5.  
    6. function Start () {
    7.  
    8. }
    9.  
    10. function Update () {
    11.  
    12. }
    13.  
    14. function OnTriggerEnter (other : Collider){
    15.     if (other.gameObject.tag == "Player") {
    16.         enter = true;
    17.         sound.active = false;
    18.         AudioSource.PlayClipAtPoint(shot, transform.position);
    19.     }
    20. }
    21.  
    22.  
    23. function OnTriggerExit (other : Collider){
    24.     if (other.gameObject.tag == "Player") {
    25.         enter = false;
    26.         sound.active = true;
    27.     }
    28. }
     
  6. Batuhan13

    Batuhan13

    Joined:
    Apr 9, 2017
    Posts:
    117
    Hi mate I know it is little bit late but I think I found a great way to do that. I am lerping audiosource.volume variable when player trigger with collider here is my code. If there is a someplaces which you dont understand you can ask me =) Good luck.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SoundManager : MonoBehaviour
    6. {
    7.     [SerializeField]private AudioSource[] _OutDoorSources;
    8.     [SerializeField] private float _LerpingSpeed = 0;
    9.  
    10.     private float[] _startingVolumes;
    11.     private bool _isPlayerInInside = false;
    12.     private bool _isCalculatingDone = false;
    13.     private float _newVolume = 0;
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         _CalculateStartingValues();
    18.     }
    19.  
    20.     private void _CalculateStartingValues()
    21.     {
    22.         _startingVolumes=new float[_OutDoorSources.Length];
    23.         for( int i = 0; i < _OutDoorSources.Length; i++)
    24.         {
    25.             _startingVolumes[i] = _OutDoorSources[i].volume;
    26.         }
    27.  
    28.         _isCalculatingDone = true;
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void Update()
    33.     {
    34.         LerpingMethod();
    35.     }
    36.  
    37.     private void LerpingMethod()
    38.     {
    39.         if (_isPlayerInInside==true)
    40.         {
    41.             DecreaseOutDoorSoundVolumes();
    42.         }
    43.         else if(_isCalculatingDone==true&&_isPlayerInInside==false)
    44.         {
    45.             IncreaseOutDoorSoundVolume();
    46.         }
    47.     }
    48.    
    49.    
    50.     private void OnTriggerEnter(Collider other)
    51.     {
    52.         if (other.gameObject.tag == "RemoveOutDoorSounds")
    53.         {
    54.             _isPlayerInInside = true;
    55.         }
    56.         else if (other.gameObject.tag == "EnableOutDoorSounds")
    57.         {
    58.             _isPlayerInInside = false;
    59.         }
    60.     }
    61.  
    62.     private void IncreaseOutDoorSoundVolume()
    63.     {
    64.         for( int i = 0; i < _OutDoorSources.Length; i++)
    65.         {
    66.             _newVolume = Mathf.Lerp(_OutDoorSources[i].volume, _startingVolumes[i], _LerpingSpeed);
    67.             _OutDoorSources[i].volume = _newVolume;
    68.         }
    69.     }
    70.    
    71.     private void DecreaseOutDoorSoundVolumes()
    72.     {
    73.             for( int i = 0; i < _OutDoorSources.Length; i++)
    74.             {
    75.                 _newVolume = Mathf.Lerp(_OutDoorSources[i].volume, 0, _LerpingSpeed);
    76.                 _OutDoorSources[i].volume = _newVolume;
    77.             }
    78.     }
    79. }
    80.