【问题标题】:Car sound with respect to speed?汽车声音与速度有关吗?
【发布时间】:2019-06-13 22:27:53
【问题描述】:

我需要根据速度改变汽车的音高。 目前我正在使用的答案是:https://answers.unity.com/questions/1067016/car-engine-sound-code-unity5car-engine-sound-code.html

public float topSpeed = 100; // km per hour
private float currentSpeed = 0;
private float pitch = 0;

void Update () 
{
     currentSpeed = transform.GetComponent<Rigidbody>().velocity.magnitude * 3.6f;
     pitch = currentSpeed / topSpeed;

     transform.GetComponent <AudioSource> ().Pitch = pitch;
}

据此,起始pitch0 它会根据我的currentSpeed 而变化 即 - currentSpeed/topSpeed 所以当我当前的 speed = topSpeed pitch 将是 1 时,这是一个好方法 但在我的情况下,它会播放声音,但是当我的车到达topSpeed 时,它会停止播放声音,即使我刹车并从零速开始也不会再播放

由于我是初学者中级,我认为这是因为我的汽车刚体是运动学的,但我不知道正确的原因和任何解决方案。

【问题讨论】:

  • 当您的当前速度在您的最高速度限制内时,音高是否会减小?
  • 当您当前的速度在您的最高速度限制范围内时,您的音高变量是否会减小?
  • 不,在最高速度时它不会降低,但是当我降低速度时它会降低但不播放任何声音
  • 你尝试过转换吗?获取组件()。 Pitch =0.5 + (currentSpeed/topSpeed)

标签: c# android unity3d audio soundeffect


【解决方案1】:

我知道现在回答为时已晚。但也许它对其他有同样问题的人有所帮助。

这是你可以用一个声音做的最简单的方法,

将此输入到车辆控制器脚本

 //Vehicle's rigidbody
 Rigidbody _rigidBody;
  
 //You have to assign in a sound
 public AudioSource _shipSound;
 float EngineAcceleration = 10f;

 private void Start()
 {
    _rigidBody = GetComponent<Rigidbody>(); 
    _SoundShip.Play();  
 }
 private void Update()
 {
   //simply we divide Rigidbody speed to ourAcceleration
  _SoundShip.pitch = Mathf.Clamp(_rigidBody.velocity.magnitude / 
  EngineAcceleration , 
  minEngineSound, maxEngineSound);
 }
 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多