Quantcast
Channel: Answers by "TheCatProblem"
Viewing all articles
Browse latest Browse all 28

Answer by TheCatProblem

$
0
0
You'll presumably be dealing with an object that has an attached [Collider][1], [Rigidbody][2], and [AudioSource][3]. Off the top of my head, here's how I would try to implement what you've described: private float thresholdSpeed = 5.5f; // Adjust this value as desired // OnCollisionStay is called once per frame for every collider that is touching another collider. void OnCollisionStay(Collision collisionInfo){ // Check if the speed of the object exceeds the threshold if (rigidbody.velocity.magnitude > thresholdSpeed){ // If the AudioSource isn't playing, start it if (!audio.isPlaying){ audio.Play(); // Probably best to enable looping of the audio clip in the AudioSource settings or via audio.loop } } else{ // Object is touching something, but isn't moving fast enough // If the AudioSource is playing, stop it if (audio.isPlaying){ audio.Pause(); // Pausing is probably better than stopping for looping sounds since it avoids always playing the loop from the start } } } I haven't tested this, but I think this general idea should work. You'd also need a status variable to indicate when the object isn't touching anything (to stop the sound if the object stops touching any other objects while still travelling at a speed above the threshold), but that could be handled using a boolean set by OnCollisionStay(). Alternatively, using [OnCollisionExit()][4] might work (just place audio.Pause() inside). At any rate, I hope that helps! [1]: http://docs.unity3d.com/ScriptReference/Collider.html [2]: http://docs.unity3d.com/ScriptReference/Rigidbody.html [3]: http://docs.unity3d.com/ScriptReference/AudioSource.html [4]: http://docs.unity3d.com/ScriptReference/Collider.OnCollisionExit.html

Viewing all articles
Browse latest Browse all 28

Trending Articles



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