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.
Connect with him on:
    Blogger
    Facebook

9 comments:

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

    ReplyDelete
    Replies
    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
      Component.
      2. Choose Event -> Event Trigger .
      3. Add New Event Type to choose a custom
      event.
      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...

      Delete
  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

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

    ReplyDelete
  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

    ReplyDelete