Add ambient sounds

This commit is contained in:
Jens Pitkänen 2020-04-20 23:20:32 +03:00
parent 07891ba69f
commit 73d328e2e0
38 changed files with 423 additions and 33 deletions

View File

@ -1057,15 +1057,13 @@ MonoBehaviour:
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
Effects:
- {fileID: 11400000, guid: 1e8a50f126551b144b536b2643d4f38c, type: 2}
- {fileID: 11400000, guid: a7ecdd1c4aa8e1b4d998c7c2b94b237c, type: 2}
- {fileID: 11400000, guid: 0247f0f621e470f419aa3b48a8ab69d8, type: 2}
Cooldown: 2
AmbientNoiseCooldown: 0
Roll: 0
--- !u!1 &6215241420411417389
GameObject:
m_ObjectHideFlags: 0

View File

@ -0,0 +1,14 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "Unnamed Ambient Sound Effect", menuName = "Campfire/Ambient Sound Effect")]
public class AmbientEffect : ScriptableObject {
public AudioClip[] Clips;
public float Chance;
public int RollsSinceLast;
public float MinY;
public float MaxY;
public float MinDistance;
public float MaxDistance;
}

View File

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

View File

@ -4,50 +4,60 @@ 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;
public AmbientEffect[] Effects;
public float Cooldown;
[Header("Runtime values")]
public float AmbientNoiseCooldown = 0f;
public float Roll = 0f;
private Transform World;
private List<float> SoundTimes = new List<float>();
private float[] CumulativeChances;
private float TotalCumulativeChance;
private int[] RollsSince;
private void Awake() {
World = GameObject.FindGameObjectWithTag("World").transform;
CumulativeChances = new float[Effects.Length];
TotalCumulativeChance = 0;
for (int I = 0; I < Effects.Length; I++) {
CumulativeChances[I] = TotalCumulativeChance;
TotalCumulativeChance += Effects[I].Chance;
}
RollsSince = new int[Effects.Length];
}
private void Update() {
AmbientNoiseCooldown -= Time.deltaTime;
if (AmbientNoiseCooldown <= 0) {
AmbientNoiseCooldown += Random.Range(MinCooldown, MaxCooldown) + TrackingDuration * Mathf.Pow(SoundTimes.Count / TooManySoundsCount, 2);
AmbientNoiseCooldown += Cooldown;
int RemoveUntil = -1;
for (int I = 0; I < SoundTimes.Count; I++) {
if (Time.time - SoundTimes[I] > TrackingDuration) {
RemoveUntil = I;
} else {
Roll = Random.Range(0, TotalCumulativeChance);
AmbientEffect Effect = null;
for (int I = CumulativeChances.Length - 1; I >= 0; I--) {
if (Roll >= CumulativeChances[I]) {
if (Effects[I].RollsSinceLast <= RollsSince[I]) {
Effect = Effects[I];
RollsSince[I] = -1;
} else {
Effect = Effects[0];
}
break;
}
}
SoundTimes.RemoveRange(0, RemoveUntil + 1);
SoundTimes.Add(Time.time);
float Distance = Random.Range(MinDistance, MaxDistance);
for (int I = 0; I < RollsSince.Length; I++) {
RollsSince[I]++;
}
float Distance = Random.Range(Effect.MinDistance, Effect.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);
Vector3 Offset = new Vector3(Mathf.Cos(Rads) * Distance, Random.Range(Effect.MinY, Effect.MaxY), 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)];
if (Sfx != null && Effect.Clips.Length > 0) {
Sfx.clip = Effect.Clips[Random.Range(0, Effect.Clips.Length)];
Sfx.volume = Mathf.Sqrt(Random.Range(0.3f, 1f));
Sfx.Play();
}

View File

@ -39,7 +39,6 @@ public class StickSpawner : MonoBehaviour {
var dir = transform.position - Player.transform.position;
if ((Vector3.Angle(Player.transform.forward, dir) > StickSpawnAngle) || dir.magnitude > StickSpawnMinDistance && Random.value <= StickSpawnChance) {
Debug.Log("Spawned");
CanSpawnNew = false;
var Stick = GameObject.Instantiate(StickPrefab, transform.position + Vector3.up * 2, Random.rotation);
SpawnedStick = Stick.GetComponent<Item>();

Binary file not shown.

View File

@ -0,0 +1,22 @@
fileFormatVersion: 2
guid: 20383dcf88da4cb4baaf9f97c5d1984b
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:

Binary file not shown.

View File

@ -0,0 +1,22 @@
fileFormatVersion: 2
guid: a4ec966f344783f4fa5b663cb08b54c9
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:

View File

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

Binary file not shown.

View File

@ -0,0 +1,22 @@
fileFormatVersion: 2
guid: 4dc9bc88db3b1a74682bb1437ddeed3c
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:

Binary file not shown.

View File

@ -0,0 +1,22 @@
fileFormatVersion: 2
guid: ae3239e7d8d60034a80b3aef44b436a1
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:

Binary file not shown.

View File

@ -0,0 +1,22 @@
fileFormatVersion: 2
guid: 135e984ffdf62b6488fc7d37f8c6a245
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:

Binary file not shown.

View File

@ -0,0 +1,22 @@
fileFormatVersion: 2
guid: 531d1f946fd14a14c86c0aa6d83a09e7
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:

Binary file not shown.

View File

@ -0,0 +1,22 @@
fileFormatVersion: 2
guid: 4af78ad9842e324438de25a998d1f158
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:

Binary file not shown.

View File

@ -0,0 +1,22 @@
fileFormatVersion: 2
guid: 354cf40ba73a7ec40bf1f3e1eb74f805
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:

Binary file not shown.

View File

@ -0,0 +1,22 @@
fileFormatVersion: 2
guid: d66f24285f602ff4fb502da9a6c19fea
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:

Binary file not shown.

View File

@ -0,0 +1,22 @@
fileFormatVersion: 2
guid: ae21821f01ba1bc40ad795ddc18f3df7
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:

Binary file not shown.

View File

@ -0,0 +1,22 @@
fileFormatVersion: 2
guid: eb3cf88d2b840e640a6f73320829e6b7
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:

View File

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

View File

@ -0,0 +1,23 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: cb283095763630c4ca0f5351c4869139, type: 3}
m_Name: Bird Takeoff
m_EditorClassIdentifier: Assembly-CSharp::AmbientEffect
Clips:
- {fileID: 8300000, guid: 20383dcf88da4cb4baaf9f97c5d1984b, type: 3}
- {fileID: 8300000, guid: a4ec966f344783f4fa5b663cb08b54c9, type: 3}
Chance: 0.3
RollsSinceLast: 1
MinY: 2
MaxY: 8
MinDistance: 5
MaxDistance: 20

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 0247f0f621e470f419aa3b48a8ab69d8
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,30 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: cb283095763630c4ca0f5351c4869139, type: 3}
m_Name: Owl
m_EditorClassIdentifier: Assembly-CSharp::AmbientEffect
Clips:
- {fileID: 8300000, guid: abcd384e5671295429f1f9e38f53c67d, type: 3}
- {fileID: 8300000, guid: 4dc9bc88db3b1a74682bb1437ddeed3c, type: 3}
- {fileID: 8300000, guid: ae3239e7d8d60034a80b3aef44b436a1, type: 3}
- {fileID: 8300000, guid: 135e984ffdf62b6488fc7d37f8c6a245, type: 3}
- {fileID: 8300000, guid: 531d1f946fd14a14c86c0aa6d83a09e7, type: 3}
- {fileID: 8300000, guid: 4af78ad9842e324438de25a998d1f158, type: 3}
- {fileID: 8300000, guid: 354cf40ba73a7ec40bf1f3e1eb74f805, type: 3}
- {fileID: 8300000, guid: d66f24285f602ff4fb502da9a6c19fea, type: 3}
- {fileID: 8300000, guid: ae21821f01ba1bc40ad795ddc18f3df7, type: 3}
- {fileID: 8300000, guid: eb3cf88d2b840e640a6f73320829e6b7, type: 3}
Chance: 0.5
MinY: 2
MaxY: 8
MinDistance: 10
MaxDistance: 70

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: a7ecdd1c4aa8e1b4d998c7c2b94b237c
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,20 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: cb283095763630c4ca0f5351c4869139, type: 3}
m_Name: Silence
m_EditorClassIdentifier: Assembly-CSharp::AmbientEffect
Clips: []
Chance: 1
MinY: 5
MaxY: 5
MinDistance: 5
MaxDistance: 5

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 1e8a50f126551b144b536b2643d4f38c
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,3 +1,6 @@
External assets used:
- Campfire sounds from the Yle archive: https://freesound.org/people/YleArkisto/sounds/299266/
- From the Yle archive:
- Campfire: https://freesound.org/people/YleArkisto/sounds/299266/
- Owl: https://freesound.org/people/YleArkisto/sounds/338716/
- Bird takeoff: https://freesound.org/people/YleArkisto/sounds/388973/
- Footsteps by revolt2563: https://freesound.org/people/revolt2563/sounds/352870/