Racing Car Game # Day 4— Braking & Skidding
Objective: Add brakes to the vehicle, skid marks, and sound effects
In the previous article, Racing Car Game # Day 3 — Steering, we added steering to the vehicle. In this article, we will add braking to the vehicle and enhance this experience by playing the skidding sound when the vehicle’s wheels’ friction exceeds a certain threshold and showing skid marks on the ground.
Braking
We will use the Space key for the brakes but will add it as a new Input. In Edit > Project Settings > Input Manager, duplicate Jump and rename the new element to Brakes.
In the Locomotion script, let’s capture this input. First, we need to set a maximum braking force.
We can play around with this as we want the vehicle to gentle slow down and not abruptly. Next, let’s capture the input and pass it to the Move method as we did with steering.
In the Move method, we want to clamp this input differently as we are only interested in values 0 to 1 and multiple this by the maximum braking force.
Now, here you can make a decision on which wheels will have brakes. In our demo, we will only apply the brakes to the rear wheels.
Save the script, head back to the Editor, and press Play. Your braking torque force will not be enough. I adjusted mine and found the 8000 works well. Remember you can change this in the Inspector.
Noticed the back wheels locking up and when we brake while reversing you will see dipping…so cool.
Skidding Sound Effect
In the Assets Store, you will find the Unity Standard Assets where I found the skid sound for this project.
With the Car selected, add an Audio Source component. Uncheck the Play On Awake and reduce the Volume to around 0.3
Now, in the Locomotion script, we will create a variable to store the skid sound effect and a reference to the Audio Source Component during the Awake method.
Drag the Skid sound to the slot on the Locomotion script
Let’s now create the logic when to play the sound effect in the script. At the top, create a variable that will store the skid threshold. Create a SkidCheck function that checks each of the wheel colliders forward and sideways slip and if they are above this threshold and we are not already playing the skid sound effect then play the sound effect.
In the end, we turn off the sound effect if there are no wheels skidding or if the sound is playing. Save the script and head back to the Editor and press Play. The skidding sound effects may not sound perfect back this will definitely get you started and maybe won’t be a big deal when other sounds come into play later like an engine revving.
Skid Marks
I wrote a huge article on this topic and just couldn’t get it right and was not happy with it. I found this awesome resource, UnitySkidmarks on Github, that generates a mesh and uses a shader to display the skid marks. Kudo to this developer!
I added the Wheel Skid script to each of the Wheel Colliders and then create an empty object named SkidMarkController and add the Skidmarks scripts on it.
Select each of the wheel colliders that now have the Wheel Skid script, component, drag on the Car that has the Rigidbody on it to the Rb slot and the SkidMarkController to the Skidmarks Controller slot.
Press Play and we now have braking, skid marks, and sound effects.
Play around with the settings and see what results you get. In the next article, we will create wheel smoke.
If you enjoyed reading this article give me a Clap, also if you would like to see more, “Follow” me, so you may be notified of future releases. You may also send me a message if you need any help or if there is something you think I can improve.