<aside> 📄 This script triggers a sound when your VR Rig moves and gives you a time variable to adjust how frequently the sound plays.
</aside>
using UnityEngine;
public class FootstepSounds : MonoBehaviour
{
public AudioClip[] footstepSounds;
public AudioSource audioSource;
public float footstepInterval = 1.0f; // Time interval between footstep sounds in seconds
private float lastFootstepTime;
private void Start()
{
audioSource = GetComponent<AudioSource>();
lastFootstepTime = Time.time;
}
private void Update()
{
// Check if enough time has passed since the last footstep
if (Time.time - lastFootstepTime >= footstepInterval)
{
PlayFootstepSound();
}
}
private void PlayFootstepSound()
{
if (footstepSounds.Length > 0)
{
AudioClip footstepClip = footstepSounds[Random.Range(0, footstepSounds.Length)];
audioSource.clip = footstepClip;
audioSource.Play();
// Update the last footstep time
lastFootstepTime = Time.time;
}
}
}