以与速度相关的间隔播放滴答声的最有效方法?
Most efficient way to play ticking sound at intervals correlated to velocity?
我有一个边缘很锋利的球,我想模拟球滚动时的点击。我将如何以与球的速度相关的间隔播放滴答声?前任。随着球速的增加,滴答声变得更加频繁。
当前可用变量:
AudioClip[] tickSounds; // the sound files
bool onGround; // indicates if the ball is on the ground
Rigidbody rb; // Rigidbody of the ball to access velocity
我试过Time.frameCount
和调制,但我无法找到好的节奏。
提前致谢。
想通了!使用协程!
void Start() {
StartCoroutines("TickSound", 30f); // method name, initial wait time
}
IEnumerator TickSound(float time) {
yield return new WaitForSeconds(time);
while (true) {
// Don't bother playing sounds if its below a certain velocity
if (onGround && rb.velocity.magnitude > 1f) {
au.PlayOneShot(tickSounds[Random.Range(0, tickSounds.Length)], 0.2f);
}
// Used this equation 0.6f / ((velocity / 3f) + 1)
yield return new WaitForSeconds(0.6f / (rb.velocity.magnitude / 3f + 1)) ;
}
}
我有一个边缘很锋利的球,我想模拟球滚动时的点击。我将如何以与球的速度相关的间隔播放滴答声?前任。随着球速的增加,滴答声变得更加频繁。
当前可用变量:
AudioClip[] tickSounds; // the sound files
bool onGround; // indicates if the ball is on the ground
Rigidbody rb; // Rigidbody of the ball to access velocity
我试过Time.frameCount
和调制,但我无法找到好的节奏。
提前致谢。
想通了!使用协程!
void Start() {
StartCoroutines("TickSound", 30f); // method name, initial wait time
}
IEnumerator TickSound(float time) {
yield return new WaitForSeconds(time);
while (true) {
// Don't bother playing sounds if its below a certain velocity
if (onGround && rb.velocity.magnitude > 1f) {
au.PlayOneShot(tickSounds[Random.Range(0, tickSounds.Length)], 0.2f);
}
// Used this equation 0.6f / ((velocity / 3f) + 1)
yield return new WaitForSeconds(0.6f / (rb.velocity.magnitude / 3f + 1)) ;
}
}