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.
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.
sorry but i don’t feel like paying 50$ for this. Anyone have any other solutions?
My first para is the diy solution. No purchase required, but takes some coding.
Ok I got it. I’m new to learning code and just made my first piece!!! (Feels so good)
#pragma strict
private var enter : boolean;
var sound : GameObject;
var shot : AudioClip;
function Start () {
}
function Update () {
}
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
enter = true;
sound.active = false;
AudioSource.PlayClipAtPoint(shot, transform.position);
}
}
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
enter = false;
sound.active = true;
}
}
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.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoundManager : MonoBehaviour
{
[SerializeField]private AudioSource[] _OutDoorSources;
[SerializeField] private float _LerpingSpeed = 0;
private float[] _startingVolumes;
private bool _isPlayerInInside = false;
private bool _isCalculatingDone = false;
private float _newVolume = 0;
// Start is called before the first frame update
void Start()
{
_CalculateStartingValues();
}
private void _CalculateStartingValues()
{
_startingVolumes=new float[_OutDoorSources.Length];
for( int i = 0; i < _OutDoorSources.Length; i++)
{
_startingVolumes[i] = _OutDoorSources[i].volume;
}
_isCalculatingDone = true;
}
// Update is called once per frame
void Update()
{
LerpingMethod();
}
private void LerpingMethod()
{
if (_isPlayerInInside==true)
{
DecreaseOutDoorSoundVolumes();
}
else if(_isCalculatingDone==true&&_isPlayerInInside==false)
{
IncreaseOutDoorSoundVolume();
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "RemoveOutDoorSounds")
{
_isPlayerInInside = true;
}
else if (other.gameObject.tag == "EnableOutDoorSounds")
{
_isPlayerInInside = false;
}
}
private void IncreaseOutDoorSoundVolume()
{
for( int i = 0; i < _OutDoorSources.Length; i++)
{
_newVolume = Mathf.Lerp(_OutDoorSources[i].volume, _startingVolumes[i], _LerpingSpeed);
_OutDoorSources[i].volume = _newVolume;
}
}
private void DecreaseOutDoorSoundVolumes()
{
for( int i = 0; i < _OutDoorSources.Length; i++)
{
_newVolume = Mathf.Lerp(_OutDoorSources[i].volume, 0, _LerpingSpeed);
_OutDoorSources[i].volume = _newVolume;
}
}
}