Quantcast
Viewing all articles
Browse latest Browse all 164

PlayOneShot on footstep script either plays very delayed or not at all.

I'm trying to create my own footstep script to control the audio clip that played every given step length. Everything I've used to trace it seems like it should work. I'm getting "step" to print reliably in the console exactly how I'd like it, and the audio clips are randomizing well in my footstep AudioSource. However, it just doesn't work. there's about a 10% chance that the audio will play at all, and when it does, there's a very low chance that it will play on time. Sometimes, it seems to cut off the sound, which I thought was impossible with playOneShot. I'd really appreciate some help on this. My whole FootstepControl code is below. All arrays currently have a length of 8 and are filled with .wav files. #pragma strict public var footstepSource : AudioSource; public var watSteps : AudioClip[]; public var mudSteps : AudioClip[]; public var libSteps : AudioClip[]; public var intSteps : AudioClip[]; var currentClip : AudioClip; var n : int; var footstepSet : String; public var fpc : CharacterController; var distanceTravelled : float = 0; var lastPosition : Vector3; public var stepLength : float; var counter : int; function Start() { lastPosition = transform.position; } function Update() { counter++; distanceTravelled += Vector3.Distance(transform.position, lastPosition); print("frame " +counter + ": distanceTravelled1 " + distanceTravelled); if(distanceTravelled > stepLength && fpc.velocity.sqrMagnitude > 0){ var p : int; print("step " + footstepSource.clip); footstepSource.PlayOneShot(footstepSource.clip, 1); playSound(); changeSounds(footstepSet); //So same sound doesn't play twice in a row; p = Random.Range(0, 7); if( n != p){ n = p; }else { n +=1; } distanceTravelled = 0; } lastPosition = transform.position; } //Change Sound as per location, set by trigger volumes. function changeSounds(stepSet : String){ if (stepSet == "water") { currentClip = watSteps[n]; } if (stepSet == "library") { currentClip = libSteps[n]; } if (stepSet == "intRoom") { currentClip = intSteps[n]; } if (stepSet == "mud") { currentClip = mudSteps[n]; } } function playSound (){ footstepSource.PlayOneShot(footstepSource.clip, 1); } Any insight is appreciated! Thanks!

Viewing all articles
Browse latest Browse all 164

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>