From 32459bec810a13a35fc3ef8100740479c2981e9d Mon Sep 17 00:00:00 2001 From: BOT Alex <44818698+MagicBOTAlex@users.noreply.github.com> Date: Sun, 4 Feb 2024 02:20:08 +0100 Subject: [PATCH] Added footsteps --- Assets/Scenes/GameScene.unity | 32 +++++++++++++++++++ .../Managers/AudioManager/AudioManager.cs | 4 ++- Assets/Scripts/Player/FootStepSounder.cs | 24 ++++++++++++++ Assets/Scripts/Player/FootStepSounder.cs.meta | 11 +++++++ 4 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 Assets/Scripts/Player/FootStepSounder.cs create mode 100644 Assets/Scripts/Player/FootStepSounder.cs.meta diff --git a/Assets/Scenes/GameScene.unity b/Assets/Scenes/GameScene.unity index 5e494a4..9a70800 100644 --- a/Assets/Scenes/GameScene.unity +++ b/Assets/Scenes/GameScene.unity @@ -1604,6 +1604,9 @@ PrefabInstance: - targetCorrespondingSourceObject: {fileID: 3120938410244321186, guid: 30e0cc55a67f02d4f92b2677ec4b1511, type: 3} insertIndex: -1 addedObject: {fileID: 1579580571} + - targetCorrespondingSourceObject: {fileID: 3120938410244321186, guid: 30e0cc55a67f02d4f92b2677ec4b1511, type: 3} + insertIndex: -1 + addedObject: {fileID: 1579580572} m_SourcePrefab: {fileID: 100100000, guid: 30e0cc55a67f02d4f92b2677ec4b1511, type: 3} --- !u!114 &869964774 stripped MonoBehaviour: @@ -1774,6 +1777,9 @@ PrefabInstance: - targetCorrespondingSourceObject: {fileID: 3120938410244321186, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} insertIndex: -1 addedObject: {fileID: 1254139636} + - targetCorrespondingSourceObject: {fileID: 3120938410244321186, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} + insertIndex: -1 + addedObject: {fileID: 1254139637} m_SourcePrefab: {fileID: 100100000, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} --- !u!114 &1232208433 stripped MonoBehaviour: @@ -1895,6 +1901,19 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: invert: 0 +--- !u!114 &1254139637 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1254139629} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d4d58ce9310f45c42af5d8003f1a832c, type: 3} + m_Name: + m_EditorClassIdentifier: + StepInterval: 0.8 --- !u!1 &1377274208 GameObject: m_ObjectHideFlags: 0 @@ -3403,6 +3422,19 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: invert: 0 +--- !u!114 &1579580572 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1579580564} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d4d58ce9310f45c42af5d8003f1a832c, type: 3} + m_Name: + m_EditorClassIdentifier: + StepInterval: 0.8 --- !u!1 &1600052931 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Managers/AudioManager/AudioManager.cs b/Assets/Scripts/Managers/AudioManager/AudioManager.cs index b532890..6194e8d 100644 --- a/Assets/Scripts/Managers/AudioManager/AudioManager.cs +++ b/Assets/Scripts/Managers/AudioManager/AudioManager.cs @@ -2,6 +2,7 @@ using System; using System.Collections; using System.Collections.Generic; using System.Linq; +using Unity.VisualScripting; using UnityEngine; @@ -35,7 +36,7 @@ public class AudioManager : MonoBehaviour return Instance.audioLibrary.Clips.First(x => x.name == nameOfClip); } - public static AudioSource PlaySound(string nameOfClip, Vector3 sourcePos, bool is3D, bool loop = false, bool fadeIn = false) + public static AudioSource PlaySound(string nameOfClip, Vector3 sourcePos, bool is3D = true, bool loop = false, bool fadeIn = false) { if (Instance == null) { @@ -48,6 +49,7 @@ public class AudioManager : MonoBehaviour audioSource.loop = loop; audioSource.clip = FindAudioClip(nameOfClip); sourceObject.transform.position = sourcePos; + sourceObject.transform.parent = Instance.transform; if (is3D) { diff --git a/Assets/Scripts/Player/FootStepSounder.cs b/Assets/Scripts/Player/FootStepSounder.cs new file mode 100644 index 0000000..ada9efa --- /dev/null +++ b/Assets/Scripts/Player/FootStepSounder.cs @@ -0,0 +1,24 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FootStepSounder : MonoBehaviour +{ + [SerializeField] private float StepInterval = 0.5f; + [SerializeField] private float Volume = 0.25f; + + private float movedDist = 0; + private Vector3 prevPos; + + // Update is called once per frame + void Update() + { + movedDist += Vector3.Distance(transform.position, prevPos); + if (movedDist % StepInterval < 0.1f) + { + AudioManager.PlaySound("Footstep_" + UnityEngine.Random.Range(0, 6), transform.position, true).volume = Volume; + } + + prevPos = transform.position; + } +} diff --git a/Assets/Scripts/Player/FootStepSounder.cs.meta b/Assets/Scripts/Player/FootStepSounder.cs.meta new file mode 100644 index 0000000..4a5b194 --- /dev/null +++ b/Assets/Scripts/Player/FootStepSounder.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d4d58ce9310f45c42af5d8003f1a832c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: