根据刚体速度更改音频音调
Change audio pitch based on rigidbody speed
当玩家移动或滚动时,我们有一些声音,因为玩家是一个球。我们希望球运动得越快,音频的音调就越高。我尝试了下面的代码,但它什么也没做。我认为这是因为 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;
}
}
您可以使用map
功能轻松控制音高值。
float mapValue(float mainValue, float inValueMin, float inValueMax, float outValueMin, float outValueMax)
{
return (mainValue - inValueMin) * (outValueMax - outValueMin) / (inValueMax - inValueMin) + outValueMin;
}
您将 AudioSource.pitch
传递给 mainValue
参数。
对于 inValueMin
值,您传入 Rigidbody.velocity.magnitude
的 default/MIN 值,即 0
。
对于inValueMax
值,你传入你的球可以到达的最大值。
您可以通过 Debug.Log("RB: " + ballRigidbody.velocity.magnitude);
和 运行 游戏轻松确定此数字。 10
似乎没问题。你必须确定自己的价值。
默认的AudioSource.pitch
值为1
,所以outValueMin
参数应该是1
.
outValueMax
参数将是您认为可以接受的最大音高。我发现 1.5
没问题,所以 1.5
将用于 outValueMax
。
你从 mapValue
函数得到的就是你分配给 AudioSource.pitch
的。这使您可以更好地控制声音的音高。您可以在 Arduino site.
上阅读有关此功能的更多信息
删除您当前的音频代码并将其替换为:
float rigidBodyMangintude = rigidbdy.velocity.magnitude;
float pitch = mapValue(rigidBodyMangintude, 0f, 10f, 1f, 1.5f);
audio.pitch = pitch;
Debug.Log("Pitch: " + pitch);
mapValue
函数位于此答案的顶部。
当玩家移动或滚动时,我们有一些声音,因为玩家是一个球。我们希望球运动得越快,音频的音调就越高。我尝试了下面的代码,但它什么也没做。我认为这是因为 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;
}
}
您可以使用map
功能轻松控制音高值。
float mapValue(float mainValue, float inValueMin, float inValueMax, float outValueMin, float outValueMax)
{
return (mainValue - inValueMin) * (outValueMax - outValueMin) / (inValueMax - inValueMin) + outValueMin;
}
您将 AudioSource.pitch
传递给 mainValue
参数。
对于 inValueMin
值,您传入 Rigidbody.velocity.magnitude
的 default/MIN 值,即 0
。
对于inValueMax
值,你传入你的球可以到达的最大值。
您可以通过 Debug.Log("RB: " + ballRigidbody.velocity.magnitude);
和 运行 游戏轻松确定此数字。 10
似乎没问题。你必须确定自己的价值。
默认的AudioSource.pitch
值为1
,所以outValueMin
参数应该是1
.
outValueMax
参数将是您认为可以接受的最大音高。我发现 1.5
没问题,所以 1.5
将用于 outValueMax
。
你从 mapValue
函数得到的就是你分配给 AudioSource.pitch
的。这使您可以更好地控制声音的音高。您可以在 Arduino site.
删除您当前的音频代码并将其替换为:
float rigidBodyMangintude = rigidbdy.velocity.magnitude;
float pitch = mapValue(rigidBodyMangintude, 0f, 10f, 1f, 1.5f);
audio.pitch = pitch;
Debug.Log("Pitch: " + pitch);
mapValue
函数位于此答案的顶部。