Search Unity

Audio Car engine sound with respect to speed

Discussion in 'Audio & Video' started by RACHITSINGH433, Jan 20, 2019.

  1. RACHITSINGH433

    RACHITSINGH433

    Joined:
    Oct 17, 2018
    Posts:
    3
    need my car sound pitch to be changed with respect to speed. Current i am using the solution of an answer which is :https://answers.unity.com/questions/1067016/car-engine-sound-code-unity5car-engine-sound-code.html

    Code (CSharp):
    1. public float topSpeed = 100; // km per hour
    2. private float currentSpeed = 0;
    3. private float pitch = 0;
    4.  
    5. void Update () {
    6.     currentSpeed = transform.GetComponent <Rigidbody>
    7.     ().velocity.magnitude * 3.6f;
    8.     pitch = currentSpeed / topSpeed;
    9.  
    10.     transform.GetComponent <AudioSource> ().Pitch =
    11.     pitch;
    12.     }
    As per this the starting pitch is 0 And it changes as per my current speed I.e - current speed/topspeed so when my current speed = top speed the pitch will be 1 and it's good approach But in my case it plays the sound but when once my car reaches top speed it stops playing sound than and Nevers plays it again even if I brake and start from zero speed

    As I am an beginner intermediate i think it's because of my rigidbody of car is kinematic but I don't know the correct reason and any solution of this

    Any kind of help will be appreciated
    For somewhat errors(i don't know why) i can't post a question on unity answers page :)
     
  2. slaczky

    slaczky

    Joined:
    Sep 26, 2015
    Posts:
    224
    Maybe it's a bit late, but I made an asset that simulates engine sounds, available in the Asset Store with engine sound packs: https://goo.gl/DCTgsb
    There is also a cheaper Lite and Plus versions too if the Pro version is too expensive for you.
     
    RACHITSINGH433 likes this.
  3. MathewHI

    MathewHI

    Joined:
    Mar 29, 2016
    Posts:
    501
    I thought I would reply because for some reason I can't post in Unity Answers either, I don't know why. Try this function
    the values will have to be changed according to your specifications.
    Code (CSharp):
    1.  
    2. float modifier = 1;
    3.  AudioSource audio;
    4.  
    5.     void UpdateGears()
    6.     {
    7.         float soundPitchDiff = 1;
    8.  
    9.         if (vm.speedInMPH <= 40)
    10.         {
    11.             //print("Gear 1");
    12.             Gear1 = true;
    13.             Gear2 = false;
    14.             Gear3 = false;
    15.             Gear4 = false;
    16.             Gear5 = false;
    17.             soundPitchDiff = 1.0f;
    18.         }
    19.  
    20.         if (vm.speedInMPH >= 70)
    21.         {
    22.             //print("Gear 2");
    23.             Gear1 = false;
    24.             Gear2 = true;
    25.             Gear3 = false;
    26.             Gear4 = false;
    27.             Gear5 = false;
    28.             soundPitchDiff = 1.3f;
    29.         }
    30.  
    31.         if (vm.speedInMPH >= 100)
    32.         {
    33.             //print("Gear 3");
    34.             Gear1 = false;
    35.             Gear2 = false;
    36.             Gear3 = true;
    37.             Gear4 = false;
    38.             Gear5 = false;
    39.             soundPitchDiff = 1.5f;
    40.         }
    41.  
    42.         if (vm.speedInMPH >= 130)
    43.         {
    44.            // print("Gear 4");
    45.             Gear1 = false;
    46.             Gear2 = false;
    47.             Gear3 = false;
    48.             Gear4 = true;
    49.             Gear5 = false;
    50.             soundPitchDiff = 2f;
    51.         }
    52.  
    53.         if (vm.speedInMPH >= 170)
    54.         {
    55.             //print("Gear 5");
    56.             Gear1 = false;
    57.             Gear2 = false;
    58.             Gear3 = false;
    59.             Gear4 = false;
    60.             Gear5 = true;
    61.             soundPitchDiff = 2.3f;
    62.         }
    63.  
    64.         audio.pitch = vm.speedInMPH / soundPitchDiff * modifier * .06f;
    65.        
    66.     }