Add ambient sound system

This commit is contained in:
Jens Pitkänen 2020-04-20 21:46:48 +03:00
parent adf6e245c2
commit a96bff5967
11 changed files with 304 additions and 3 deletions

View File

@ -0,0 +1,143 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &5362017482645399226
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 6620256556670932985}
- component: {fileID: 8570857400429686574}
- component: {fileID: 4367637972437939355}
m_Layer: 0
m_Name: Ambient Sound
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &6620256556670932985
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5362017482645399226}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &8570857400429686574
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5362017482645399226}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 60f36e8849a468f45beadd2d5d390309, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!82 &4367637972437939355
AudioSource:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5362017482645399226}
m_Enabled: 1
serializedVersion: 4
OutputAudioMixerGroup: {fileID: -8678847571685279384, guid: 032de345950918c4e89684078c6a7213,
type: 2}
m_audioClip: {fileID: 0}
m_PlayOnAwake: 0
m_Volume: 1
m_Pitch: 1
Loop: 0
Mute: 0
Spatialize: 0
SpatializePostEffects: 0
Priority: 128
DopplerLevel: 1
MinDistance: 1
MaxDistance: 500
Pan2D: 0
rolloffMode: 0
BypassEffects: 0
BypassListenerEffects: 0
BypassReverbZones: 0
rolloffCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 1
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
- serializedVersion: 3
time: 1
value: 0
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
panLevelCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 1
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
spreadCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 0
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
reverbZoneMixCustomCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 1
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 2b1540a91810a824f95d38d8a1dce667
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -851,6 +851,7 @@ GameObject:
- component: {fileID: 7093134810128755097}
- component: {fileID: 462937706053753531}
- component: {fileID: 8702400494665140028}
- component: {fileID: 1070985534702310852}
m_Layer: 11
m_Name: Player
m_TagString: Player
@ -1042,6 +1043,29 @@ MonoBehaviour:
m_EditorClassIdentifier:
Loraces: []
ChunkKeepAliveTransformsParent: {fileID: 3141763353809520921}
--- !u!114 &1070985534702310852
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6125707630481988399}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 799107a14d3c0d5499f9fa1df5ddd71c, type: 3}
m_Name:
m_EditorClassIdentifier:
AmbientSfxPrefab: {fileID: 5362017482645399226, guid: 2b1540a91810a824f95d38d8a1dce667,
type: 3}
MinDistance: 4
MaxDistance: 15
Clips:
- {fileID: 8300000, guid: abcd384e5671295429f1f9e38f53c67d, type: 3}
MinCooldown: 1
MaxCooldown: 8
TooManySoundsCount: 4
TrackingDuration: 14
AmbientNoiseCooldown: 0
--- !u!1 &6215241420411417389
GameObject:
m_ObjectHideFlags: 0

View File

@ -0,0 +1,56 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AmbientSoundGenerator : MonoBehaviour {
public GameObject AmbientSfxPrefab;
public float MinDistance;
public float MaxDistance;
public AudioClip[] Clips;
[Header("Cooldown configuration")]
public float MinCooldown;
public float MaxCooldown;
public int TooManySoundsCount;
[Tooltip("")]
public float TrackingDuration;
[Header("Runtime values")]
public float AmbientNoiseCooldown = 0f;
private Transform World;
private List<float> SoundTimes = new List<float>();
private void Awake() {
World = GameObject.FindGameObjectWithTag("World").transform;
}
private void Update() {
AmbientNoiseCooldown -= Time.deltaTime;
if (AmbientNoiseCooldown <= 0) {
AmbientNoiseCooldown += Random.Range(MinCooldown, MaxCooldown) + TrackingDuration * Mathf.Pow(SoundTimes.Count / TooManySoundsCount, 2);
int RemoveUntil = -1;
for (int I = 0; I < SoundTimes.Count; I++) {
if (Time.time - SoundTimes[I] > TrackingDuration) {
RemoveUntil = I;
} else {
break;
}
}
SoundTimes.RemoveRange(0, RemoveUntil + 1);
SoundTimes.Add(Time.time);
float Distance = Random.Range(MinDistance, MaxDistance);
float Rads = Random.Range(0, 2 * Mathf.PI);
Vector3 Offset = new Vector3(Mathf.Cos(Rads) * Distance, Random.Range(0f, 5f), Mathf.Sin(Rads) * Distance);
GameObject Obj = Instantiate(AmbientSfxPrefab, transform.position + Offset, new Quaternion(), World);
AudioSource Sfx = Obj.GetComponent<AudioSource>();
if (Sfx != null) {
Sfx.clip = Clips[Random.Range(0, Clips.Length)];
Sfx.volume = Mathf.Sqrt(Random.Range(0.3f, 1f));
Sfx.Play();
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 799107a14d3c0d5499f9fa1df5ddd71c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -112,14 +112,17 @@ public class ItemGrabber : MonoBehaviour {
}
}
PrimaryIndicator.alpha = Mathf.Lerp(PrimaryIndicator.alpha, Item != null || Campfire != null ? 1 : 0, 10f * Time.deltaTime);
if (Campfire != null && GrabbedItems.Count > 0) {
PrimaryText.text = $"Burn {GrabbedItems[0].Quality.DisplayName}";
PrimaryIndicator.alpha = Mathf.Lerp(PrimaryIndicator.alpha,
Item != null || Campfire != null || ThrowableItem != null ? 1 : 0,
10f * Time.deltaTime);
if (Campfire != null && ThrowableItem != null) {
PrimaryText.text = $"Burn {ThrowableItem.Quality.DisplayName}";
} else if (Item != null) {
PrimaryText.text = $"Take {Item.Quality.DisplayName}";
} else if (ThrowableItem != null) {
PrimaryText.text = $"Throw {ThrowableItem.Quality.DisplayName}";
}
SecondaryIndicator.alpha = Mathf.Lerp(SecondaryIndicator.alpha, CanIgniteTorch ? 1 : 0, 10f * Time.deltaTime);
if (CanIgniteTorch) {
SecondaryText.text = $"Ignite {TorchableItem.Quality.DisplayName}";

View File

@ -0,0 +1,16 @@
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class SelfDestructAfterPlaying : MonoBehaviour {
private AudioSource Source;
private void Awake() {
Source = GetComponent<AudioSource>();
}
private void LateUpdate() {
if (!Source.isPlaying) {
Destroy(gameObject);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 60f36e8849a468f45beadd2d5d390309
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5e8f766bf7981754c85f5a1afb7365bb
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,22 @@
fileFormatVersion: 2
guid: abcd384e5671295429f1f9e38f53c67d
AudioImporter:
externalObjects: {}
serializedVersion: 6
defaultSettings:
loadType: 0
sampleRateSetting: 0
sampleRateOverride: 44100
compressionFormat: 1
quality: 1
conversionMode: 0
platformSettingOverrides: {}
forceToMono: 0
normalize: 1
preloadAudioData: 1
loadInBackground: 0
ambisonic: 0
3D: 1
userData:
assetBundleName:
assetBundleVariant: