我有一个有锋利边缘的球,我想在球滚动时模拟点击。如何以与球速相关的间隔播放滴答声?防爆。随着球速的增加,滴答声变得更加频繁。
目前可用的变量:
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
和调制,但我无法获得良好的节奏。
提前致谢。
答案 0 :(得分:1)
想出来!使用Coroutines!
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)) ;
}
}