some sound bug fixes
This commit is contained in:
@@ -28,6 +28,7 @@ public class CarDrivingRoutine : NetworkBehaviour
|
||||
|
||||
private EventInstance CarMovement;
|
||||
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
//Debug.LogError("AUDIO MANAGER:");
|
||||
@@ -39,6 +40,7 @@ public class CarDrivingRoutine : NetworkBehaviour
|
||||
CarMovement = AudioManager.Instance.CreateInstance(FMODEvents.Instance.BoltCarSimpleDriving); //initialising the variable
|
||||
CarMovement.setParameterByName("EasyBoltLogic", 0); //"Driving - 0 in FMOD"
|
||||
CarMovement.set3DAttributes(FMODUnity.RuntimeUtils.To3DAttributes(gameObject)); //setting 3d attributes
|
||||
|
||||
}
|
||||
private void Start()
|
||||
{
|
||||
|
||||
@@ -123,7 +123,7 @@ public class KeyboardManager : NetworkBehaviour
|
||||
|
||||
void OnEnterPressed()
|
||||
{
|
||||
AudioManager.Instance.PlayAttachedInstance(FMODEvents.Instance.Hover, gameObject);
|
||||
AudioManager.Instance.PlayAttachedInstance(FMODEvents.Instance.Click, gameObject);
|
||||
|
||||
if (_input.Length > 0)
|
||||
{
|
||||
|
||||
@@ -9,7 +9,11 @@ public class FloorButtonVisualizer : MonoBehaviour
|
||||
public Sprite ActiveSprite;
|
||||
public bool ActiveState;
|
||||
private Image buttonImage;
|
||||
// Start is called before the first frame update
|
||||
|
||||
// --- Static tracking for selection change ---
|
||||
private static FloorButtonVisualizer lastActiveButton = null;
|
||||
private static bool initialized = false;
|
||||
|
||||
void Start()
|
||||
{
|
||||
buttonImage = gameObject.GetComponent<Image>();
|
||||
@@ -19,12 +23,20 @@ public class FloorButtonVisualizer : MonoBehaviour
|
||||
{
|
||||
this.ActiveState = true;
|
||||
buttonImage.sprite = ActiveSprite;
|
||||
//Debug.Log("Floorbutton of " + gameObject.name + " activated");
|
||||
|
||||
// --- Only play hover if selection actually changed ---
|
||||
if (initialized && lastActiveButton != this)
|
||||
{
|
||||
AudioManager.Instance.PlayAttachedInstance(FMODEvents.Instance.Hover, gameObject);
|
||||
}
|
||||
|
||||
lastActiveButton = this;
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
public void Deactivate()
|
||||
{
|
||||
this.ActiveState = false;
|
||||
buttonImage.sprite = InactiveSprite;
|
||||
//Debug.Log("Floorbutton of " + gameObject.name + " deactivated");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,9 @@ public class MenuTeleportButton : MonoBehaviour
|
||||
private EventInstance TeleportingSound;
|
||||
FMOD.Studio.Bus SpecialBus; //FMOD bus variable
|
||||
|
||||
private static MenuTeleportButton lastSelectedButton = null;
|
||||
private static bool initialized = false;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
TeleportingSound = AudioManager.Instance.CreateInstance(FMODEvents.Instance.Teleport); //initialise the instance
|
||||
@@ -58,12 +61,19 @@ public class MenuTeleportButton : MonoBehaviour
|
||||
|
||||
public void SetStateSelected()
|
||||
{
|
||||
AudioManager.Instance.PlayAttachedInstance(FMODEvents.Instance.Hover, gameObject);
|
||||
if (button != null && HoverSprite != null)
|
||||
{
|
||||
button.targetGraphic.GetComponent<Image>().sprite = HoverSprite;
|
||||
|
||||
// --- Only play hover sound if selection actually changed ---
|
||||
if (initialized && lastSelectedButton != this)
|
||||
{
|
||||
AudioManager.Instance.PlayAttachedInstance(FMODEvents.Instance.Hover, gameObject);
|
||||
}
|
||||
|
||||
lastSelectedButton = this;
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void SetStateDefault()
|
||||
@@ -106,7 +116,7 @@ public class MenuTeleportButton : MonoBehaviour
|
||||
button.interactable = false;
|
||||
button.interactable = true;
|
||||
|
||||
StartCoroutine(MuteBusForSeconds(1.5f));
|
||||
StartCoroutine(MuteBusForSeconds(2.0f));
|
||||
|
||||
TeleportingSound.start(); //playing 2d oneshot
|
||||
}
|
||||
|
||||
@@ -14,10 +14,16 @@ public class TutorialAudioListener : MonoBehaviour
|
||||
private TeleportationProvider teleportationProvider;
|
||||
|
||||
private float lastStepTime;
|
||||
[SerializeField] private float baseStepCooldown = 0.5f; // base interval at speed = 1
|
||||
private float stepCooldown;
|
||||
|
||||
[Header("Settings")]
|
||||
[SerializeField] private float baseStepRate = 0.5f; // Step interval at joystick = 1 and slider = 1
|
||||
[SerializeField] private float joystickThreshold = 0.1f; // Minimum stick movement to count as walking
|
||||
|
||||
private float settingsSpeedMultiplier = 1f; // From slider (1 = default)
|
||||
private bool locomotionEnabled = false;
|
||||
|
||||
private Vector2 currentMoveVector;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (tutorialController == null)
|
||||
@@ -32,14 +38,15 @@ public class TutorialAudioListener : MonoBehaviour
|
||||
turnAction = tutorialController.turnProvider.rightHandSnapTurnAction.action;
|
||||
teleportationProvider = tutorialController.teleportProvider;
|
||||
}
|
||||
|
||||
stepCooldown = baseStepCooldown;
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (moveAction != null)
|
||||
moveAction.performed += OnMovePerformed;
|
||||
{
|
||||
moveAction.performed += OnMoveInput;
|
||||
moveAction.canceled += OnMoveCanceled;
|
||||
}
|
||||
|
||||
if (turnAction != null)
|
||||
turnAction.performed += OnTurnPerformed;
|
||||
@@ -57,7 +64,10 @@ public class TutorialAudioListener : MonoBehaviour
|
||||
private void OnDisable()
|
||||
{
|
||||
if (moveAction != null)
|
||||
moveAction.performed -= OnMovePerformed;
|
||||
{
|
||||
moveAction.performed -= OnMoveInput;
|
||||
moveAction.canceled -= OnMoveCanceled;
|
||||
}
|
||||
|
||||
if (turnAction != null)
|
||||
turnAction.performed -= OnTurnPerformed;
|
||||
@@ -75,26 +85,48 @@ public class TutorialAudioListener : MonoBehaviour
|
||||
private void HandleLocomotionToggled(bool enabled)
|
||||
{
|
||||
locomotionEnabled = enabled;
|
||||
Debug.Log($"[TutorialAudioListener] Locomotion toggled: {enabled}");
|
||||
}
|
||||
|
||||
private void HandleSpeedChanged(float newSpeed)
|
||||
{
|
||||
// Invert proportionality: faster speed = shorter step interval
|
||||
float calculatedCooldown = baseStepCooldown / Mathf.Max(newSpeed, 0.01f);
|
||||
|
||||
// Clamp the result between 0.4 and 0.6 seconds
|
||||
stepCooldown = Mathf.Clamp(calculatedCooldown, 0.3f, 0.69f);
|
||||
|
||||
Debug.Log($"[TutorialAudioListener] Step cooldown adjusted: {stepCooldown}");
|
||||
// Slider is allowed to range normally but we clamp for stability
|
||||
settingsSpeedMultiplier = Mathf.Clamp(newSpeed, 0.4f, 1.2f);
|
||||
}
|
||||
|
||||
private void OnMovePerformed(InputAction.CallbackContext context)
|
||||
private void OnMoveInput(InputAction.CallbackContext ctx)
|
||||
{
|
||||
currentMoveVector = ctx.ReadValue<Vector2>();
|
||||
}
|
||||
|
||||
private void OnMoveCanceled(InputAction.CallbackContext ctx)
|
||||
{
|
||||
currentMoveVector = Vector2.zero;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
TryPlayStep();
|
||||
}
|
||||
|
||||
private void TryPlayStep()
|
||||
{
|
||||
if (!locomotionEnabled)
|
||||
return;
|
||||
|
||||
if (Time.time - lastStepTime < stepCooldown)
|
||||
float joystickMagnitude = currentMoveVector.magnitude;
|
||||
|
||||
if (joystickMagnitude < joystickThreshold)
|
||||
return; // Not moving enough to walk
|
||||
|
||||
// --- NEW: Slider influence (very light touch) ---
|
||||
float sliderInfluence = 1f + (settingsSpeedMultiplier) * 0.25f;
|
||||
float effectiveMagnitude = joystickMagnitude * sliderInfluence;
|
||||
|
||||
// --- NEW: Step cooldown ---
|
||||
float dynamicCooldown = baseStepRate / Mathf.Max(effectiveMagnitude, 0.01f);
|
||||
dynamicCooldown = Mathf.Clamp(dynamicCooldown, 0.33f, 0.65f);
|
||||
|
||||
if (Time.time - lastStepTime < dynamicCooldown)
|
||||
return;
|
||||
|
||||
lastStepTime = Time.time;
|
||||
@@ -103,13 +135,16 @@ public class TutorialAudioListener : MonoBehaviour
|
||||
|
||||
private void OnTurnPerformed(InputAction.CallbackContext context)
|
||||
{
|
||||
// If magnitude above threshold = walking don't play spin sound
|
||||
if (currentMoveVector.magnitude > joystickThreshold)
|
||||
return;
|
||||
|
||||
// Otherwise play turning sound
|
||||
AudioManager.Instance.PlayAttachedInstance(FMODEvents.Instance.StepSpin, gameObject);
|
||||
}
|
||||
|
||||
private void OnTeleportEnd(LocomotionSystem locomotionSystem)
|
||||
{
|
||||
|
||||
// Optional: Uncomment if teleport should play sound
|
||||
// AudioManager.Instance.PlayAttachedInstance(FMODEvents.Instance.Steps, gameObject);
|
||||
// Optional teleport sound
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3221,6 +3221,7 @@ GameObject:
|
||||
- component: {fileID: 2567477070120516364}
|
||||
- component: {fileID: 5667922335807455518}
|
||||
- component: {fileID: 9130699439852735294}
|
||||
- component: {fileID: 244862221083601452}
|
||||
m_Layer: 5
|
||||
m_Name: Level 1 button
|
||||
m_TagString: Untagged
|
||||
@@ -3346,6 +3347,35 @@ MonoBehaviour:
|
||||
InactiveSprite: {fileID: 21300000, guid: 9b9161f51c7cdb84eba98c454b3f542c, type: 3}
|
||||
ActiveSprite: {fileID: 21300000, guid: 594b1f27343305e4e99551b5fb8a1b76, type: 3}
|
||||
ActiveState: 0
|
||||
--- !u!114 &244862221083601452
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 4006117569149960393}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: d0b148fe25e99eb48b9724523833bab1, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
m_Delegates:
|
||||
- eventID: 0
|
||||
callback:
|
||||
m_PersistentCalls:
|
||||
m_Calls:
|
||||
- m_Target: {fileID: 9130699439852735294}
|
||||
m_TargetAssemblyTypeName: FloorButtonVisualizer, Assembly-CSharp
|
||||
m_MethodName: Activate
|
||||
m_Mode: 1
|
||||
m_Arguments:
|
||||
m_ObjectArgument: {fileID: 0}
|
||||
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
|
||||
m_IntArgument: 0
|
||||
m_FloatArgument: 0
|
||||
m_StringArgument:
|
||||
m_BoolArgument: 0
|
||||
m_CallState: 2
|
||||
--- !u!1 &4122378368372937094
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -3395,6 +3425,7 @@ GameObject:
|
||||
- component: {fileID: 735405659653074444}
|
||||
- component: {fileID: 1877344747539671532}
|
||||
- component: {fileID: 2993125651756248496}
|
||||
- component: {fileID: 5719623585416982853}
|
||||
m_Layer: 5
|
||||
m_Name: Level 2 button
|
||||
m_TagString: Untagged
|
||||
@@ -3520,6 +3551,35 @@ MonoBehaviour:
|
||||
InactiveSprite: {fileID: 21300000, guid: 28748e86c9a1f0c4694006461434eca7, type: 3}
|
||||
ActiveSprite: {fileID: 21300000, guid: a84b9455a04a3ca459595ecb5810fb98, type: 3}
|
||||
ActiveState: 1
|
||||
--- !u!114 &5719623585416982853
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 4441188026596657465}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: d0b148fe25e99eb48b9724523833bab1, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
m_Delegates:
|
||||
- eventID: 0
|
||||
callback:
|
||||
m_PersistentCalls:
|
||||
m_Calls:
|
||||
- m_Target: {fileID: 2993125651756248496}
|
||||
m_TargetAssemblyTypeName: FloorButtonVisualizer, Assembly-CSharp
|
||||
m_MethodName: Activate
|
||||
m_Mode: 1
|
||||
m_Arguments:
|
||||
m_ObjectArgument: {fileID: 0}
|
||||
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
|
||||
m_IntArgument: 0
|
||||
m_FloatArgument: 0
|
||||
m_StringArgument:
|
||||
m_BoolArgument: 0
|
||||
m_CallState: 2
|
||||
--- !u!1 &4552840262979230797
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -7700,7 +7760,9 @@ MonoBehaviour:
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
tutorialController: {fileID: 8296457994299276779}
|
||||
stepCooldown: 0.4
|
||||
locomotionConfigurator: {fileID: 0}
|
||||
baseStepRate: 0.5
|
||||
joystickThreshold: 0.1
|
||||
--- !u!4 &4954755430420460766 stripped
|
||||
Transform:
|
||||
m_CorrespondingSourceObject: {fileID: 1894017693134941767, guid: d90913a7f0b00844a8c4482b2afc2c66,
|
||||
|
||||
Binary file not shown.
164
Assets/_PROJECT/Scripts/Audio/FirstPersonOcclusion.cs
Normal file
164
Assets/_PROJECT/Scripts/Audio/FirstPersonOcclusion.cs
Normal file
@@ -0,0 +1,164 @@
|
||||
using UnityEngine;
|
||||
using FMODUnity;
|
||||
using FMOD.Studio;
|
||||
using System.Collections;
|
||||
|
||||
public class FirstPersonOcclusion : MonoBehaviour
|
||||
{
|
||||
[Header("FMOD Event")]
|
||||
[SerializeField] private EventReference SelectAudio;
|
||||
|
||||
private EventInstance AudioOccluded;
|
||||
private EventDescription AudioDes;
|
||||
private StudioListener Listener;
|
||||
private PLAYBACK_STATE pb;
|
||||
|
||||
[Header("Occlusion Options")]
|
||||
[SerializeField][Range(0f, 10f)] private float SoundOcclusionWidening = 1f;
|
||||
[SerializeField][Range(0f, 10f)] private float PlayerOcclusionWidening = 1f;
|
||||
[SerializeField] private LayerMask OcclusionLayer;
|
||||
|
||||
private bool AudioIsVirtual;
|
||||
private float MaxDistance;
|
||||
private float ListenerDistance;
|
||||
private float lineCastHitCount = 0f;
|
||||
private Color colour;
|
||||
|
||||
private bool initialisedExternally = false;
|
||||
|
||||
// ============================================================
|
||||
// NEW <20> Optional external initialiser
|
||||
// ============================================================
|
||||
public void InitialiseWithInstance(EventInstance instance)
|
||||
{
|
||||
AudioOccluded = instance;
|
||||
initialisedExternally = true;
|
||||
|
||||
RuntimeManager.AttachInstanceToGameObject(
|
||||
AudioOccluded,
|
||||
gameObject,
|
||||
GetComponent<Rigidbody>()
|
||||
);
|
||||
|
||||
AudioOccluded.getDescription(out AudioDes);
|
||||
AudioDes.getMinMaxDistance(out float min, out MaxDistance);
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// ORIGINAL START <20> kept exactly the same unless external init used
|
||||
// ============================================================
|
||||
private IEnumerator Start()
|
||||
{
|
||||
// If already initialised, skip internal creation
|
||||
if (!initialisedExternally)
|
||||
{
|
||||
AudioOccluded = RuntimeManager.CreateInstance(SelectAudio);
|
||||
RuntimeManager.AttachInstanceToGameObject(AudioOccluded, gameObject);
|
||||
AudioOccluded.start();
|
||||
AudioOccluded.release();
|
||||
|
||||
AudioDes = RuntimeManager.GetEventDescription(SelectAudio);
|
||||
AudioDes.getMinMaxDistance(out float minDistance, out MaxDistance);
|
||||
}
|
||||
|
||||
// Find listener (kept exactly as before)
|
||||
yield return new WaitUntil(() => FindObjectOfType<StudioListener>() != null);
|
||||
Listener = FindObjectOfType<StudioListener>();
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// UNCHANGED core functionality
|
||||
// ============================================================
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (Listener == null) return;
|
||||
|
||||
AudioOccluded.isVirtual(out AudioIsVirtual);
|
||||
AudioOccluded.getPlaybackState(out pb);
|
||||
ListenerDistance = Vector3.Distance(transform.position, Listener.transform.position);
|
||||
|
||||
if (!AudioIsVirtual && pb == PLAYBACK_STATE.PLAYING && ListenerDistance <= MaxDistance)
|
||||
{
|
||||
OccludeBetween(transform.position, Listener.transform.position);
|
||||
}
|
||||
|
||||
lineCastHitCount = 0f;
|
||||
}
|
||||
|
||||
private void OccludeBetween(Vector3 sound, Vector3 listener)
|
||||
{
|
||||
Vector3 SoundLeft = CalculatePoint(sound, listener, SoundOcclusionWidening, true);
|
||||
Vector3 SoundRight = CalculatePoint(sound, listener, SoundOcclusionWidening, false);
|
||||
|
||||
Vector3 ListenerLeft = CalculatePoint(listener, sound, PlayerOcclusionWidening, true);
|
||||
Vector3 ListenerRight = CalculatePoint(listener, sound, PlayerOcclusionWidening, false);
|
||||
|
||||
Vector3 SoundAbove = new Vector3(sound.x, sound.y + SoundOcclusionWidening, sound.z);
|
||||
Vector3 SoundBelow = new Vector3(sound.x, sound.y - SoundOcclusionWidening, sound.z);
|
||||
Vector3 ListenerAbove = new Vector3(listener.x, listener.y + PlayerOcclusionWidening * .5f, listener.z);
|
||||
Vector3 ListenerBelow = new Vector3(listener.x, listener.y - PlayerOcclusionWidening * .5f, listener.z);
|
||||
|
||||
CastLine(SoundLeft, ListenerLeft);
|
||||
CastLine(SoundLeft, listener);
|
||||
CastLine(SoundLeft, ListenerRight);
|
||||
|
||||
CastLine(sound, ListenerLeft);
|
||||
CastLine(sound, listener);
|
||||
CastLine(sound, ListenerRight);
|
||||
|
||||
CastLine(SoundRight, ListenerLeft);
|
||||
CastLine(SoundRight, listener);
|
||||
CastLine(SoundRight, ListenerRight);
|
||||
|
||||
CastLine(SoundAbove, ListenerAbove);
|
||||
CastLine(SoundBelow, ListenerBelow);
|
||||
|
||||
colour = (PlayerOcclusionWidening == 0f || SoundOcclusionWidening == 0f) ? Color.blue : Color.green;
|
||||
|
||||
SetParameter();
|
||||
}
|
||||
|
||||
private Vector3 CalculatePoint(Vector3 a, Vector3 b, float m, bool posOrneg)
|
||||
{
|
||||
float n = Vector3.Distance(new Vector3(a.x, 0f, a.z), new Vector3(b.x, 0f, b.z));
|
||||
if (n == 0f) return a;
|
||||
|
||||
float mn = (m / n);
|
||||
float x, z;
|
||||
|
||||
if (posOrneg)
|
||||
{
|
||||
x = a.x + (mn * (a.z - b.z));
|
||||
z = a.z - (mn * (a.x - b.x));
|
||||
}
|
||||
else
|
||||
{
|
||||
x = a.x - (mn * (a.z - b.z));
|
||||
z = a.z + (mn * (a.x - b.x));
|
||||
}
|
||||
|
||||
return new Vector3(x, a.y, z);
|
||||
}
|
||||
|
||||
private void CastLine(Vector3 Start, Vector3 End)
|
||||
{
|
||||
RaycastHit hit;
|
||||
bool isHit = Physics.Linecast(Start, End, out hit, OcclusionLayer);
|
||||
|
||||
if (isHit)
|
||||
{
|
||||
lineCastHitCount++;
|
||||
Debug.DrawLine(Start, End, Color.red);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.DrawLine(Start, End, colour);
|
||||
}
|
||||
}
|
||||
|
||||
private void SetParameter()
|
||||
{
|
||||
float occlusionValue = lineCastHitCount / 11;
|
||||
AudioOccluded.setParameterByName("Occlusion", occlusionValue);
|
||||
}
|
||||
}
|
||||
@@ -1,195 +0,0 @@
|
||||
using UnityEngine;
|
||||
using FMODUnity;
|
||||
using FMOD.Studio;
|
||||
using System.Collections;
|
||||
|
||||
public class FirstPersonOcclusion : MonoBehaviour
|
||||
{
|
||||
[Header("FMOD Event")]
|
||||
[SerializeField]
|
||||
private FMODUnity.EventReference SelectAudio;
|
||||
private EventInstance AudioOccluded;
|
||||
private EventDescription AudioDes;
|
||||
private StudioListener Listener;
|
||||
private PLAYBACK_STATE pb;
|
||||
|
||||
[Header("Occlusion Options")]
|
||||
[SerializeField]
|
||||
[Range(0f, 10f)]
|
||||
private float SoundOcclusionWidening = 1f;
|
||||
[SerializeField]
|
||||
[Range(0f, 10f)]
|
||||
private float PlayerOcclusionWidening = 1f;
|
||||
[SerializeField]
|
||||
private LayerMask OcclusionLayer;
|
||||
|
||||
private bool AudioIsVirtual;
|
||||
private float MaxDistance;
|
||||
private float ListenerDistance;
|
||||
private float lineCastHitCount = 0f;
|
||||
private Color colour;
|
||||
|
||||
private IEnumerator Start()
|
||||
{
|
||||
Debug.Log("--- Start Method ---");
|
||||
|
||||
// 1. Event Instance Creation
|
||||
AudioOccluded = RuntimeManager.CreateInstance(SelectAudio);
|
||||
//Debug.Log($"Created FMOD Event Instance for: {SelectAudio.Path}");
|
||||
|
||||
// 2. Attaching Instance
|
||||
RuntimeManager.AttachInstanceToGameObject(AudioOccluded, gameObject);
|
||||
Debug.Log($"Attached FMOD Event Instance to GameObject: {gameObject.name}");
|
||||
|
||||
// 3. Starting Audio
|
||||
AudioOccluded.start();
|
||||
Debug.Log("Started FMOD Event Instance.");
|
||||
|
||||
// 4. Releasing Instance (This allows the event to self-manage its lifetime, which is fine)
|
||||
AudioOccluded.release();
|
||||
Debug.Log("Released FMOD Event Instance.");
|
||||
|
||||
// 5. Getting Event Description and Max Distance
|
||||
AudioDes = RuntimeManager.GetEventDescription(SelectAudio);
|
||||
AudioDes.getMinMaxDistance(out float minDistance, out MaxDistance);
|
||||
Debug.Log($"FMOD Event Min/Max Distance: {minDistance:F2} / {MaxDistance:F2}");
|
||||
|
||||
// 6. Finding Listener
|
||||
yield return new WaitUntil(() => FindObjectOfType<StudioListener>() != null);
|
||||
Listener = FindObjectOfType<StudioListener>();
|
||||
Debug.Log($"FMOD StudioListener found on {Listener.gameObject.name}");
|
||||
|
||||
Debug.Log("--- End Start Method ---");
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
// Debug.Log("--- FixedUpdate Method ---"); // Too frequent, only log conditions
|
||||
if (Listener == null) return; // Skip until listener exists
|
||||
|
||||
AudioOccluded.isVirtual(out AudioIsVirtual);
|
||||
AudioOccluded.getPlaybackState(out pb);
|
||||
ListenerDistance = Vector3.Distance(transform.position, Listener.transform.position);
|
||||
|
||||
// 7. Check Occlusion Condition
|
||||
if (!AudioIsVirtual && pb == PLAYBACK_STATE.PLAYING && ListenerDistance <= MaxDistance)
|
||||
{
|
||||
Debug.Log($"Occlusion Check: Not Virtual, Playing, Distance {ListenerDistance:F2} <= Max {MaxDistance:F2}. Calling OccludeBetween.");
|
||||
OccludeBetween(transform.position, Listener.transform.position);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Log reasons why occlusion is NOT being checked
|
||||
if (AudioIsVirtual) Debug.Log("Occlusion Skipped: Audio is Virtual.");
|
||||
if (pb != PLAYBACK_STATE.PLAYING) Debug.Log($"Occlusion Skipped: Playback State is not PLAYING. State: {pb}");
|
||||
if (ListenerDistance > MaxDistance) Debug.Log($"Occlusion Skipped: Distance {ListenerDistance:F2} > Max Distance {MaxDistance:F2}.");
|
||||
}
|
||||
|
||||
// 8. Reset Hit Count
|
||||
// Debug.Log($"Resetting lineCastHitCount from {lineCastHitCount:F0} to 0.");
|
||||
lineCastHitCount = 0f;
|
||||
}
|
||||
|
||||
private void OccludeBetween(Vector3 sound, Vector3 listener)
|
||||
{
|
||||
Debug.Log("--- OccludeBetween Method ---");
|
||||
|
||||
// 9. Calculate Points (Log only a few to avoid clutter)
|
||||
Vector3 SoundLeft = CalculatePoint(sound, listener, SoundOcclusionWidening, true);
|
||||
Vector3 SoundRight = CalculatePoint(sound, listener, SoundOcclusionWidening, false);
|
||||
// Debug.Log($"Sound Positions: Center {sound}, Left {SoundLeft}, Right {SoundRight}");
|
||||
|
||||
Vector3 ListenerLeft = CalculatePoint(listener, sound, PlayerOcclusionWidening, true);
|
||||
Vector3 ListenerRight = CalculatePoint(listener, sound, PlayerOcclusionWidening, false);
|
||||
// Debug.Log($"Listener Positions: Center {listener}, Left {ListenerLeft}, Right {ListenerRight}");
|
||||
|
||||
Vector3 SoundAbove = new Vector3(sound.x, sound.y + SoundOcclusionWidening, sound.z);
|
||||
Vector3 SoundBelow = new Vector3(sound.x, sound.y - SoundOcclusionWidening, sound.z);
|
||||
Vector3 ListenerAbove = new Vector3(listener.x, listener.y + PlayerOcclusionWidening * 0.5f, listener.z);
|
||||
Vector3 ListenerBelow = new Vector3(listener.x, listener.y - PlayerOcclusionWidening * 0.5f, listener.z);
|
||||
|
||||
// 10. Casting Lines (The line casts themselves will log hits)
|
||||
CastLine(SoundLeft, ListenerLeft);
|
||||
CastLine(SoundLeft, listener);
|
||||
CastLine(SoundLeft, ListenerRight);
|
||||
|
||||
CastLine(sound, ListenerLeft);
|
||||
CastLine(sound, listener);
|
||||
CastLine(sound, ListenerRight);
|
||||
|
||||
CastLine(SoundRight, ListenerLeft);
|
||||
CastLine(SoundRight, listener);
|
||||
CastLine(SoundRight, ListenerRight);
|
||||
|
||||
CastLine(SoundAbove, ListenerAbove);
|
||||
CastLine(SoundBelow, ListenerBelow);
|
||||
|
||||
if (PlayerOcclusionWidening == 0f || SoundOcclusionWidening == 0f)
|
||||
{
|
||||
colour = Color.blue;
|
||||
}
|
||||
else
|
||||
{
|
||||
colour = Color.green;
|
||||
}
|
||||
|
||||
SetParameter();
|
||||
Debug.Log("--- End OccludeBetween Method ---");
|
||||
}
|
||||
|
||||
private Vector3 CalculatePoint(Vector3 a, Vector3 b, float m, bool posOrneg)
|
||||
{
|
||||
// Debug.Log($"Calculating offset point for: {a} to {b} with magnitude {m} and posOrneg {posOrneg}");
|
||||
float x;
|
||||
float z;
|
||||
// n is the 2D distance between a and b
|
||||
float n = Vector3.Distance(new Vector3(a.x, 0f, a.z), new Vector3(b.x, 0f, b.z));
|
||||
float mn = (m / n);
|
||||
|
||||
// Safety check for division by zero (if sound and listener are exactly on top of each other horizontally)
|
||||
if (n == 0f)
|
||||
{
|
||||
// If points are on the same XZ position, just return the point 'a'
|
||||
// Debug.LogWarning("CalculatePoint: Division by zero avoided. Sound and Listener are at the same XZ position.");
|
||||
return a;
|
||||
}
|
||||
|
||||
if (posOrneg)
|
||||
{
|
||||
x = a.x + (mn * (a.z - b.z));
|
||||
z = a.z - (mn * (a.x - b.x));
|
||||
}
|
||||
else
|
||||
{
|
||||
x = a.x - (mn * (a.z - b.z));
|
||||
z = a.z + (mn * (a.x - b.x));
|
||||
}
|
||||
return new Vector3(x, a.y, z);
|
||||
}
|
||||
|
||||
private void CastLine(Vector3 Start, Vector3 End)
|
||||
{
|
||||
RaycastHit hit;
|
||||
// 11. Raycast result
|
||||
bool isHit = Physics.Linecast(Start, End, out hit, OcclusionLayer);
|
||||
|
||||
if (isHit)
|
||||
{
|
||||
lineCastHitCount++;
|
||||
Debug.Log($"Linecast HIT! Hit Count: {lineCastHitCount:F0}/11. Object hit: {hit.collider.name}.");
|
||||
Debug.DrawLine(Start, End, Color.red);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.DrawLine(Start, End, colour);
|
||||
}
|
||||
}
|
||||
|
||||
private void SetParameter()
|
||||
{
|
||||
float occlusionValue = lineCastHitCount / 11; // 11 is the total number of line casts
|
||||
// 12. Final Parameter Value
|
||||
Debug.Log($"Setting FMOD Parameter 'Occlusion' to: {occlusionValue:F2} (Hits: {lineCastHitCount:F0}/11)");
|
||||
AudioOccluded.setParameterByName("Occlusion", occlusionValue);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user