Hill Climb Racing Like 2D Car Physics 3 - Add Engine Sound

In the first two posts of this series where in we are creating a Hill Climb Racing Game like 2D car physics in a side view, we learned, how to create a physically stable 2D Car and added the functionality of controlling it via C# script. In this post we will add engine sound effect to our 2D Car.

The first thing you need to do is to attach an Audio Source to the CarBody object. To do this simply select the CarBody in the Hierarchy and click on Add Component and search Audio Source. In case you are wondering what is an Audio Source and what does it do?

Audio Source, as the name suggests is the source which generates Audio. You need to have an Audio Listener in your scene as well, which, obviously would listen to the Audio. By default, you will have an Audio Listener attached to your Main Camera. In case, you have deleted the Main Camera and added another camera, do add Audio Listener component to it.

This Audio Source attached to the CarBody would expect you to give it an Audio Clip which is to be played. Download this engine sound Audio Clip ( or you can add your own made engine sound clip as well ) and add it to the the Audio Clip field of the audio source.

Audio Listener attached to Main Camera

Audio Source attached to CarBody

Create a New C# Script under the Scripts folder and name it as EngineSound. Attach this script to the CarBody object. Open the script and add the below code to it:

using UnityEngine;
using System.Collections;

public class EngineSound : MonoBehaviour {
 //audiosource reference
 private AudioSource carSound;
 //the range for audio source pitch
 private const float lowPtich = 0.5f;
 private const float highPitch = 5f;

 //change the reductionFactor to 0.1f if you are using the rigidbody velocity as parameter to determine the pitch
 private const float reductionFactor = .001f;

 //Rigidbody2D carRigidbody;
 private float userInput;
 //wheeljoint2d reference
 WheelJoint2D wj;
 void Awake ()
                //get the Audio Source component attached to the car
                carSound = GetComponent<AudioSource>();
  //get the wheelJoint2D component attached to the car
  wj = GetComponent<WheelJoint2D>();
  //carRigidbody = GetComponent<Rigidbody2D>();
 void FixedUpdate()
  //get the userInput
  userInput = Input.GetAxis("Horizontal");
  //get the absolute value of jointSpeed
  float forwardSpeed =Mathf.Abs(wj.jointSpeed);
  //float forwardSpeed = transform.InverseTransformDirection(carRigidbody.velocity).x;
  //calculate the pitch factor which will be added to the audio source
  float pitchFactor = Mathf.Abs (forwardSpeed * reductionFactor * userInput) ;
  //clamp the calculated pitch factor between lowPitch and highPitch
  carSound.pitch = Mathf.Clamp (pitchFactor, lowPtich, highPitch);

Save the script and return to Unity and test the scene. On doing so, you would notice that the car engine has an idle engine sound. On pressing the left or right arrow keys, you would notice that the car has a nice acceleration sound which increases as the speed increases. And obviously it will be clamped to the highPitch and lowPitch values.

There is one more thing you would have noticed while testing the scene, the pitch of the audio moves back to the idle engine sound after a fraction of a second when you do not press the keys anymore. It is just like what happens in reality, you accelerate and the engine makes sound. If you remove your foot from the acceleration pedal, it doesn't make that sound anymore.

This is all we had in this post. I'm not sure if I would be posting anymore posts in this series, but yeah I'll see you around.

Share on Google+

About Sujit Horakeri

Sujit Horakeri is a game freak just like any other next door guy you would come across. He is a Web Developer by Profession, Game Developer by Choice.


  1. Hey, how to convert getaxis to new UI mobile button? thank you

    1. You can add a button and add a Pointer Down event to that button.. You can define a class variable to store the input value and modify it in your Pointer down event method..

      To add the event follow below steps:
      1. At the bottom of the inspector, click Add
      2. Choose Event -> Event Trigger .
      3. Add New Event Type to choose a custom
      4. The event you want is called Pointer Down.

      Write a method for the pointer down as per your requirements where you change the input values from 1 to 0 and a similar function on holding the brake button where in the value would vary from 0 to -1...

  2. Hey Sujit. Nice to work on your tutorial. It was so good until I added WheelJointCarMovement.cs to the car body. As soon as I run the editor the car goes flip forward gradually. I have done your tutorial in Unity 5.0.3 free version. Do I need to tweak the script as the unity physics engine has been changed since the launch of Unity 5. Please let me know. Eager to fix this. Thank you

  3. Sujit you are awesome, nice tutorial i have completed successfully.

  4. It is important to pull the entryway board and after that unclip the two clasps and afterward embed the new auto speaker into the space that is given. It is critical to choose the auto speaker that will be of the same size as the past auto speaker.go to my site

  5. When playing this game you will take care of your own farm. In order to advance to higher levels you will need to plant and harvest crops, invite friends to join you and hire workers to help you with the farm. This is a fun game for those who want to be farmers but live in the city. Taruhan Online

  6. Despite the twisted nature of this game, it can be quite humorous when Crypto goes on rampages, along with creating hysteria among citizens, harassed cows, destroys buildings using his saucer, as well as battling government agents trying to stop Crypto. Bandar sakong

  7. When you purchase a car subwoofer, there are four factors that you have to take into consideration: system power, subwoofer enclosure, available space and crossovers. 15 inch subwoofer

  8. it is easy to look for high-quality SOUNDEXPERTS.ORG/BEST-6x9-SPEAKERS systems regardless of whether it is for replacement purposes or just to have your current program upgraded.

  9. To take web based amusements from these stimulation menus we need to confront some basic inquiries. Each parent is especially strained about their youngsters' obsessiveness for PC gaming and web based gaming.https://eunblocked.com/run-3/

  10. I think the admin of this web page is really working hard in support of his web page, as here every material is quality based stuff.
    Football Games io Games Unblocked Games

  11. Regardless of all this, there are certain special considerations that people need to seriously look into in ensuring that they have the best sound system to suit their needs.AmplifierExperts

  12. Surround sound systems use the study for psychoacoustics which is the way people perceive and hear sound through the human ears. read more at audiogeekhub.com

  13. In either case you are risking damage to the stereo, dash, or other equipment behind the dash. This can cause more expense than the Stereo originally cost in repair bills.Remote Car Starter Akron

  14. A sign of the measure of sound that will leave your speakers on its getting a flag is dictated by its affectability rating. soundexperts

  15. There are numerous top notch sound speakers accessible in the market today.Acura Tire PSI

  16. This is cool post and i enjoy to read this post. your blog is fantastic and you have good staff in your blog. nice sharing keep it up. http://ampleom.postagon.com/546f2enzn