meta xr, auto fix, occlusion additions

This commit is contained in:
Timur Nizamov
2025-12-09 21:37:52 +02:00
parent 647d247c81
commit 1f500f31d4
164 changed files with 1314 additions and 508 deletions

View File

@@ -24,6 +24,9 @@ public class CarDrivingRoutine : NetworkBehaviour
private bool stopSoundPlayed = false;
private float stopThreshold = 0.05f; // consider speed "0" when below this
private bool isBeingStoppedByPlayer = false;
[Header("Tires")]
public List<GameObject> FrontTires;
public List<GameObject> BackTires;
@@ -54,25 +57,26 @@ public class CarDrivingRoutine : NetworkBehaviour
rollTires(); // Just an aesthetic improvement.
if (Quaternion.Angle(transform.rotation, desiredRotation) > 1f) // If the car is turning.
if (!isBeingStoppedByPlayer)
{
if (!isTurning)
if (Quaternion.Angle(transform.rotation, desiredRotation) > 1f)
{
AudioController.SetRPM(1300); //set externally in CarAudioController.cs
setTireRotation(tireTurnAngle);
if (!isTurning)
{
setTireRotation(tireTurnAngle);
AudioController.SetRPM(1300);
}
isTurning = true;
}
isTurning = true;
}
if (Quaternion.Angle(transform.rotation, desiredRotation) <= 1f) // Reset the turn value.
{
if (isTurning)
else
{
AudioController.SetRPM(1450);
setTireRotation(-tireTurnAngle);
if (isTurning)
{
setTireRotation(-tireTurnAngle);
AudioController.SetRPM(1450);
}
isTurning = false;
}
isTurning = false;
}
// Check if close enough to the waypoint
@@ -85,12 +89,14 @@ public class CarDrivingRoutine : NetworkBehaviour
if (StraightSpeed <= stopThreshold && !stopSoundPlayed)
{
stopSoundPlayed = true;
// Play the sound normally from your audio controller
//AudioController.SetRPM(475);
AudioController.PlayStopSound();
float currentRPM = AudioController.GetCurrentRPM();
//Debug.LogError(currentRPM);
if (currentRPM <= 550f) // confirms we are in the correct engine state
{
stopSoundPlayed = true;
AudioController.PlayStopSound();
//Debug.LogError("piiks");
}
}
}
@@ -120,29 +126,29 @@ public class CarDrivingRoutine : NetworkBehaviour
}
private void OnTriggerEnter(Collider other)
{;
{
if (other.GetComponentInParent<XRPlayerMirror>() == null) return;
StartCoroutine(SmoothAdjustSpeed(0, 0, haltspeed)); // Smoothly halt in 1 second
//_tireSound.Stop();
//_stopSound.Play();
isBeingStoppedByPlayer = true;
StartCoroutine(SmoothAdjustSpeed(0, 0, haltspeed));
AudioController.SetRPM(475);
//AudioController.PlayStopSound();
}
private void OnTriggerExit(Collider other)
{
if (other.GetComponentInParent<XRPlayerMirror>() == null) return;
StartCoroutine(SmoothAdjustSpeed(targetSpeed, targetRotationSpeed, haltspeed)); // Smoothly resume speed in 1 second
//_stopSound.Stop();
//_tireSound.Play();
isBeingStoppedByPlayer = false;
StartCoroutine(SmoothAdjustSpeed(targetSpeed, targetRotationSpeed, haltspeed));
AudioController.SetRPM(1450);
stopSoundPlayed = false;
}
private IEnumerator SmoothAdjustSpeed(float targetStraightSpeed, float targetRotationSpeed, float duration)
{
float initialStraightSpeed = StraightSpeed;

View File

@@ -4,6 +4,7 @@ using FMOD.Studio;
public class CarAudioController : MonoBehaviour
{
private EventInstance carMovementInstance;
private EventInstance carStopInstance;
private FirstPersonOcclusion occlusion;
private void Awake()
@@ -15,13 +16,21 @@ public class CarAudioController : MonoBehaviour
//Debug.LogError("Car Simple Driving:");
//Debug.LogError(FMODEvents.Instance.BoltCarSimpleDriving);
carMovementInstance = AudioManager.Instance.CreateInstance(FMODEvents.Instance.CarModulatedDriving);
carStopInstance = AudioManager.Instance.CreateInstance(FMODEvents.Instance.BoltCarStopSound);
carMovementInstance.setParameterByName("RPM", 1450);
carMovementInstance.setParameterByName("Load", 0);
carMovementInstance.set3DAttributes(FMODUnity.RuntimeUtils.To3DAttributes(gameObject));
carStopInstance.set3DAttributes(FMODUnity.RuntimeUtils.To3DAttributes(gameObject));
occlusion = GetComponent<FirstPersonOcclusion>();
if (occlusion != null)
occlusion.InitialiseWithInstance(carMovementInstance);
{
occlusion.InitialiseWithInstance(carMovementInstance); // main looping engine sound
occlusion.AddInstance(carStopInstance); // additional sound
}
}
private void Start()
@@ -34,6 +43,7 @@ public class CarAudioController : MonoBehaviour
{
// Always update 3D position to follow the car model transform
carMovementInstance.set3DAttributes(FMODUnity.RuntimeUtils.To3DAttributes(gameObject));
carStopInstance.set3DAttributes(FMODUnity.RuntimeUtils.To3DAttributes(gameObject));
}
// These methods are called externally by CarDrivingRoutine
@@ -44,8 +54,13 @@ public class CarAudioController : MonoBehaviour
public void PlayStopSound()
{
AudioManager.Instance.PlayAttachedInstance(FMODEvents.Instance.BoltCarStopSound, gameObject);
carStopInstance.start();
//AudioManager.Instance.PlayAttachedInstance(FMODEvents.Instance.BoltCarStopSound, gameObject);
}
public float GetCurrentRPM()
{
carMovementInstance.getParameterByName("RPM", out float value);
return value;
}
}

View File

@@ -2,6 +2,7 @@ using UnityEngine;
using FMODUnity;
using FMOD.Studio;
using System.Collections;
using System.Collections.Generic; // ADDED
public class FirstPersonOcclusion : MonoBehaviour
{
@@ -26,6 +27,10 @@ public class FirstPersonOcclusion : MonoBehaviour
private bool initialisedExternally = false;
// ADDED: now stores ALL event instances that must be occluded
private List<EventInstance> managedInstances = new List<EventInstance>();
public void InitialiseWithInstance(EventInstance instance)
{
AudioOccluded = instance;
@@ -37,9 +42,28 @@ public class FirstPersonOcclusion : MonoBehaviour
GetComponent<Rigidbody>()
);
managedInstances.Add(AudioOccluded); // ADDED
AudioOccluded.getDescription(out AudioDes);
AudioDes.getMinMaxDistance(out float min, out MaxDistance);
}
// ADDED: allows adding extra FMOD instances for occlusion
public void AddInstance(EventInstance instance)
{
if (instance.isValid())
{
RuntimeManager.AttachInstanceToGameObject(
instance,
gameObject,
GetComponent<Rigidbody>()
);
managedInstances.Add(instance);
}
}
private IEnumerator Start()
{
// If already initialised, skip internal creation
@@ -47,13 +71,18 @@ public class FirstPersonOcclusion : MonoBehaviour
if (!initialisedExternally)
{
AudioOccluded = RuntimeManager.CreateInstance(SelectAudio);
// 2. Attaching Instance
RuntimeManager.AttachInstanceToGameObject(AudioOccluded, gameObject);
// 3. Starting Audio
AudioOccluded.start();
// 4. Releasing Instance (This allows the event to self-manage its lifetime, which is fine)
AudioOccluded.release();
managedInstances.Add(AudioOccluded); // ADDED
// 5. Getting Event Description and Max Distance
AudioDes = RuntimeManager.GetEventDescription(SelectAudio);
AudioDes.getMinMaxDistance(out float minDistance, out MaxDistance);
@@ -64,6 +93,7 @@ public class FirstPersonOcclusion : MonoBehaviour
Listener = FindObjectOfType<StudioListener>();
}
private void FixedUpdate()
{
if (Listener == null) return;
@@ -82,6 +112,7 @@ public class FirstPersonOcclusion : MonoBehaviour
lineCastHitCount = 0f;
}
private void OccludeBetween(Vector3 sound, Vector3 listener)
{
// 9. Calculate Points (Log only a few to avoid clutter)
@@ -117,6 +148,7 @@ public class FirstPersonOcclusion : MonoBehaviour
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));
@@ -139,9 +171,11 @@ public class FirstPersonOcclusion : MonoBehaviour
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);
@@ -156,10 +190,18 @@ public class FirstPersonOcclusion : MonoBehaviour
}
}
private void SetParameter()
{
float occlusionValue = lineCastHitCount / 11;
// 12. Final Parameter Value
AudioOccluded.setParameterByName("Occlusion", occlusionValue);
foreach (var inst in managedInstances)
{
if (inst.isValid())
{
inst.setParameterByName("Occlusion", occlusionValue);
}
}
}
}