【发布时间】:2017-04-14 10:02:59
【问题描述】:
当玩家移动或滚动时,我们会发出一些声音,因为玩家是一个球。我们希望球跑得越快,音频的音高就越大。我尝试了下面的代码,但它没有做任何事情。我认为这是因为 p 的值太小了。 我记得在某处读到有内置的东西可以处理这个问题,但我想不出我在哪里看到的或者它叫什么。
提前致谢!
void FixedUpdate()
{
#if UNITY_EDITOR || UNITY_STANDALONE
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 move = new Vector3(-moveHorizontal, 0.0f, -moveVertical);
move = move * (speed / 15f);
//maxSpeed = maxSpeed / 5;
#else
// Player movement in mobile devices
// Building of force vector
Vector3 move = new Vector3(-Input.acceleration.x, 0.0f, -Input.acceleration.y);
// Adding force to rigidbody
move = move * (speed / 15f);
//move = movement * speed * Time.deltaTime;
#endif
rigidbdy.AddForce(move);
var p = rigidbdy.velocity.magnitude / speed;
audio.pitch = Mathf.Clamp(p, 1.0f, 2.0f); // p is clamped to sane values
//Limits the max speed
if (rigidbdy.velocity.magnitude > maxSpeed)
{
rigidbdy.velocity = rigidbdy.velocity.normalized * maxSpeed;
}
}
【问题讨论】: