Car controller tutorial #2: Car Engine Audio(realistic) in Unity + GitHub link
Summary
TLDRThis video tutorial demonstrates how to create a realistic engine sound system for a car in a video game, complete with starting, running, and reverse sounds. The guide covers setting up audio sources, looping sounds, and adjusting pitch and volume based on car speed and gas input. It also introduces a script to control the audio, including a rev limiter effect and a starting sound for the engine. Viewers can look forward to a future episode on implementing a gear system for enhanced realism.
Takeaways
- 🔊 The video demonstrates how to create a car sound system with engine sounds for a car game, including start, running, and reverse sounds.
- 🔨 It starts by creating an audio source for the engine and placing it near the engine in the game environment.
- 🔄 The script involves duplicating the audio source to differentiate between idle and running sounds, ensuring they loop for continuous play.
- 📝 A new script called 'engine audio' is created to control the audio behavior based on the car's state and speed.
- 🔊 The script references the car's speed and gas pedal input to adjust the engine sound's volume and pitch dynamically.
- 🚗 The car's speed is calculated by considering the wheel RPM and radius, which is then used to modulate the engine sound.
- 🚦 A max speed variable is introduced to limit the car's speed and prevent the engine sound from exceeding a certain pitch.
- 🎛️ The script includes a function to calculate the speed ratio, which is used to adjust the engine sound based on the gas pedal input and car speed.
- 🔧 A rev limiter effect is implemented to emulate the engine cutting off power at high speeds, using a sine wave oscillation.
- 🚀 The video also covers how to create a starting sound for the engine and transition between different engine states (not running, starting, running).
- 🔁 For reverse sounds, the script checks the direction of the car's movement and adjusts the audio source accordingly to play reverse-specific sounds.
Q & A
What is the purpose of the video?
-The purpose of the video is to demonstrate how to create an engine sound system for a car in a game without gears, featuring starting, running, and reverse sounds.
What are the three main sounds included in the sound system?
-The three main sounds included are the starting sound, running sound, and reverse sound.
How many audio sources are initially created for the engine sound system?
-Initially, two audio sources are created: one for the running sound and one for the idle sound, which is a duplicate of the first.
Why is it necessary to duplicate the audio source for the running sound?
-Duplicating the audio source allows for separate audio clips to be assigned for different engine states, such as running and idle.
What does the 'Loop' checkbox do in the audio source settings?
-The 'Loop' checkbox ensures that the audio clips will repeat continuously, simulating the continuous sound of an engine.
What is the role of the 'engine audio' script in the car?
-The 'engine audio' script is responsible for controlling the audio sources and adjusting the engine sounds based on the car's movement and user input.
How is the car's speed determined in the script?
-The car's speed is determined by calculating the wheel RPM times the wheel's radius, adjusted by the wheel's circumference and divided by 10 for a more manageable number.
What is the purpose of the 'get speed ratio' function in the script?
-The 'get speed ratio' function calculates a value between 0 and 1 that represents the ratio of the current speed to the maximum speed, taking into account the gas pedal input.
How does the script handle the engine sound when the car reaches its top speed?
-The script uses a 'speed clamped' variable to limit the engine sound, preventing it from creating a 'cracking noise' when the car reaches its maximum speed.
What is the rev limiter and how is it implemented in the sound system?
-The rev limiter is a feature that emulates the behavior of a car's engine cutting off power at high RPMs. It is implemented by adding an oscillating sound effect to the running sound pitch based on a set speed ratio threshold.
How does the script handle the starting sound of the engine?
-The script includes a 'StartCoroutine' function that plays the starting sound, sets the engine state to 'starting', and after a delay, changes the state to 'running'.
What adjustments are made to the script to create the reverse sound?
-The script checks the speed sign (positive or negative) to determine if the car is moving forward or in reverse. It then sets the appropriate audio volumes for running and reverse sounds based on this determination.
How does the script ensure the reverse sound only plays when the car is in reverse?
-The script uses a variable to check if the car's wheels are moving backward by taking the absolute value of the speed and comparing it with the sign of the speed.
What is the final step to ensure the reverse sound works correctly?
-The final step is to set the reverse volume to zero when the car is not in reverse and adjust the reverse sound parameters for a more realistic effect.
Outlines
🔊 Car Engine Sound System Creation
The video tutorial begins by introducing the project of creating a car sound system for a video game without gears. It covers the process of adding engine sounds for starting, running, and reversing. The creator demonstrates how to set up audio sources for the car's engine and idle sounds, ensuring they loop continuously. A new script is introduced to manage the audio, where references to the audio files are made, and max volume levels are set. The tutorial also explains how to adjust the pitch of the engine sound as the car accelerates.
🛠️ Implementing Speed and Torque Control
This section delves into the technical aspects of linking the car's speed and gas pedal input to the audio system. The script references the car's speed and gas pedal input to create a ratio that adjusts the engine sound accordingly. A function called 'get speed ratio' is introduced to calculate the engine's usage level. The tutorial also covers how to apply motor torque based on the car's speed, ensuring that it does not exceed a set maximum speed. The audio system is then fine-tuned to reflect the car's speed and gas input, adjusting both volume and pitch to create a realistic engine sound effect.
🔧 Addressing Engine Sound Clipping
The creator addresses an issue where the engine sound clips at high speeds. To resolve this, the tutorial introduces a method to clamp the sound, emulating a rev limiter's effect on the engine sound. Changes are made to the power controller script to calculate a 'speed clamped' variable, which is used to adjust the engine sound more smoothly. The pitch of the engine sound is also adjusted using a lerp function for a more natural sound as the car approaches its speed limit.
🎵 Enhancing the Rev Limiter Effect
The tutorial continues by enhancing the rev limiter effect on the engine sound. Additional variables are introduced to control the rev limiter's max volume, frequency, and engagement point. A sine wave function is used to create an oscillating effect on the pitch, simulating the rev limiter's behavior. The creator makes several adjustments to the script to achieve a more realistic sound, including changing the frequency of the oscillation and the point at which the rev limiter engages.
🚗 Adding a Starting Sound for the Engine
In this part of the tutorial, the creator adds a starting sound to the car's engine. The car controller script is updated to include an 'is engine running' state, which controls the application of motor torque. A new coroutine is introduced to manage the engine's starting process, including playing the starting sound and setting the engine state to 'running' after a short delay. The tutorial also explains how to ensure the starting sound plays only once and how to adjust the audio source to play the correct sound files.
🔁 Creating Reverse Sound and Final Adjustments
The final part of the tutorial focuses on creating a reverse sound for the car. The script is adjusted to check the direction of the car's movement and to play the appropriate sound files for forward motion or reversing. The car controller is updated to handle the reverse state, and the audio system is fine-tuned to ensure the reverse sound plays correctly. The creator makes several adjustments to the script to fix issues with the sound and to improve the overall audio experience. The tutorial concludes with a preview of the next video, which will cover the creation of a gear system for the car.
Mindmap
Keywords
💡Sound System
💡Audio Source
💡Looping
💡Script
💡Car Controller
💡Speed Ratio
💡Rev Limiter
💡Starting Sound
💡Reverse Sound
💡Max Speed
💡Pitch
Highlights
Creating a sound system for a car in a game without gears, including starting, running, and reverse sounds.
Demonstration of the sound system in action with engine sounds for different car states.
Setting up an audio source for the car's engine and ensuring the sounds loop for continuous play.
Creating a script named 'engine audio' to control the audio behavior of the car.
Adjusting the pitch and volume of engine sounds based on the car's speed and gas pedal input.
Using the car controller to get the actual speed of the wheels for realistic engine sound effects.
Implementing a function to calculate the speed ratio of the car for dynamic sound adjustments.
Adding a rev limiter effect to the engine sound to emulate a real car's performance at high speeds.
Creating a starting sound for the car engine and scripting the transition from off to running state.
Using Unity's coroutine to manage the engine start sequence with a delay for realism.
Addressing issues with the initial implementation of the starting sound and engine state.
Developing a reverse sound for the car and scripting the conditions for when it should play.
Fine-tuning the reverse sound to ensure it complements the car's motion and enhances the gaming experience.
Incorporating a sign check to determine the direction of the car's movement for accurate sound playback.
Adjusting the car controller's logic to correctly handle the car's speed and direction for sound effects.
Previewing the final sound system, including starting, running, and reverse sounds, in the game environment.
Announcement of the next video, which will cover the creation of a gear system for the car game.
Transcripts
so in this video I'll be showing you how
to create a sound system for your car so
an engine sound system without gears for
your car game that will have a starting
sound running sound and reverse sound so
let's see how it sounds so we start the
engine and we go forward
[Music]
okay we leave The Gas We press the gas
again
now we stop it we stop the car and we Go
reverse
and now that's the reverse sound
so forward
with a red lifter
with stop
and we can also go reverse
so yeah that's it okay so first thing we
want to do is create an audio source for
our engine to do that we go to our car
and we create a new empty object which
we will call running sound we put that
around the engine or place close to it
and we also add an audio source to it
now we duplicate that so that we have
our idle sound as well and now we want
to put our audio of Clips to our sources
so the running sound should go to our
audio
for running sound the idle should go to
our idle
we also
we also want to make sure that uh our
sounds are looping so we need to click
this Loop here
and our running sound to loop as well
okay so now what we do is create a new
script in our car which we will call uh
audio no engine audio
okay you can see the script that's all
yep so here's the script for now
and now we can open it
okay so now we want to reference our two
audio files I have sources so we go to
our hidden script and call a public
audio Source this will go running sound
we also want to have a max volume so we
will have a float that we will call Max
sorry running Max sound
max volume
and since we also want to have a new hey
so we want to change the pitch of the
engine as well we won't have a running
Max Beach
and we do that exact same thing for uh
our idle as well let me change the names
a little bit
okay
okay so we did that for arrival as well
we just change the names
I'll sound
idle running
all right so what we want to do now is
uh make sure that whenever our car is
actually moving forward uh and the cast
pedal is pressing that will actually
produce a sound so to do that we also
want to reference our
car controller so we will reference it
here
and we will also get this from the get
component script in unity so
when started to get the car controller
okay so now we want to actually get the
speed of the vehicle and uh how much gas
pedal is present so uh before we do
anything with the audio we want to go to
our uh
car controller and get the actual speed
of the vehicle oh sorry not the speed of
the vehicle but actually the speed of
the wheels that are spinning since we
don't have a gear system yet we will get
the speed of the wheels and we will
convert it to engine sound
and to do that we go to our car
controller
and we go to our speed a variable and we
change it that instead we that we use
the velocity we use the
wheel RPM times its radius so sorry
times its circumference
so that we get the actual wheel speed
okay so to do that we call the colliders
of our wheels so we get our rear right
wheel we can't use any wheel or the
average of them times the RPM
times the radius of the wheels so
that's radius times two times
uh Pi which is uh the formula for this
conference and since we don't want this
to be a huge uh number we will divide it
by 10.
okay so now that we have our speed we
also want to know the max speed of the
vehicle so we will create a new variable
in our car which we will call public
float a max speed
and we will also limit the car's speed
to that speed so uh if the speed is
higher than the than our actual speed
sorry it's an actual Max Speed then we
will not apply any motor torque so we go
to our apply motor and we say if the
speed
is uh higher than max speed
so if it's lower than our Max Speed then
we can actually apply some motor torque
if it's not
then we the motor torque is zero
so we take this
and we actually say that's zero
okay
and now we want to also create a
function that actually gives the ratio
of the gas pedal times the speed divided
by our Max Speed so that we get zero to
one value of uh how much engine are we
using all right so we create a new
public function which will return float
and uh it will be called the get speed
ratio
and so we want to actually get our gas
input as well so that's going to be uh
the clamp of the
gas pedal from 0.5
to 1.
and we're gonna also give it our gas
input
so that actually emits our gas to be
from 0.5 to 1 so it's actually uh half
of the gas input and now uh we want to
actually
return
our speed times our gas divided by our
max speed
perfect so now we go back to our audio
source and we actually change the sound
of the audio to monster speed okay so uh
now we want to actually check what the
ratio is so we create a new float which
we will call uh speed ratio
and now we want to reference that speed
ratio so we check that our car
controller is actually there
and if it is uh the speed ratio will be
equal to the
speed that we have so the
car controller dot get Speedways here
perfect so now we actually change the
volume of the sounds so we will set the
alt the idle sound the volume to be
equal to
uh the
the in between of
1.0.1 F and our actual uh
say idle Max sound
and the between value will be our actual
speed so
that's that
also we want to have our running sound
to be equal to our speed times our gas
which we that's basically our speed
ratio and if we Redline our car so we
are at maximum engine speed to actually
uh
move the pitch back and forth so that it
actually sounds like it's cutting of
power and it's giving power it's cutting
of power it's giving power as well like
a regular rev limiter will do so to do
that uh we actually want to actually
change our running
uh sound volume to be equal to the in
between of uh 0.3 F and our
Max ironic sound
and the sound will be also equal to our
speed ratio
and now the pitch
will be equal
to our uh in between again uh
how far uh
running pitch
sorry over 0.3 F again and our running
Mac speech
and that's going to be equal to our
speed ratio times the
sorry yeah no actually times the actual
uh speed ratio
and let's try that and next we'll do the
rev limiter
let's see if that works
let's set up everything uh running sound
idle sound
and also we want to set our max volume
to 1 our Max speeds to two again
also we don't need the max speed for
idle since we are not going to change
the idle pitch so that's going to delete
it
and let's see if it works
okay let's see if it works now we will
put the max speed here to 50. and so
let's check if it works
okay so we can definitely hear the
suspensions out right now
and if we press the gas
we can actually hear that
but the problem here is uh that if you
can hear there is this cracking nose is
when the car is reaching its top speed
okay and obviously we don't want that
so to fix that we want to clamp the
sound so when it cuts off the engine
power it basically emulates however rev
limiter works and it doesn't just cut
off the actual uh sound of the engine
alright so to do that we need to go back
to our power controller and change the
way we use the speed so we create a new
variable which we will call speed
clamped
so uh private sorry
load speed clubs
and we go back to our update and we
create we set this speed clamp variable
to be equal to the massive.lurk DOT
speedclamp
sorry
uh two
uh actual value of our speed
so to this so speed
and it's time to Delta time
which means how fast the frame took and
we actually use that variable instead of
our speed for our get speed ratio
okay we can also go to our engine audio
and instead of changing immediately the
pitch we can change it to its actual
self so it's also dampened so we create
a new mapf.herb we call it sorry the
first variable will be our Mac our
running sound Dot Peach
our second will be our actual page that
we want to achieve and finally we will
also use our time.delt attack
and let's see if this works
okay let's see
so we break the gas
it's accelerating
[Music]
reaching a limit where uh once you reach
this limit it doesn't go any further so
the pitch doesn't change any further so
it doesn't create a weird sound so now
what we want to do is also create the
rev limiter sound to do that we go back
to arranging audio and we create three
variables which we will call rev limiter
max volume frequency
yeah I think that's two and uh let's see
what else we will create
and uh at what variable the rev limiter
engages
so that's going to be public float
a limiter
a sound which we will set to one f for
now uh
the limiter frequency
the frequency that it oscillates
which we will set to 3F for now and uh
the public
load limiter engage
which is going to be 0.8 F which means
that if our speed ratio is 0.8 F and
higher the revenue limiter will engage
sorry if the Rev If the ratio
is higher than 0.85 sorry that our
limiter engage
let's make fix this typo
then our Revenue limiter
will be so we also need to have a rev
limiter loadable
okay so our rev limiter
will be equal to our math f
DOT sign which will be our uh which is
the basically the sign uh
function uh that oscillates up and down
like a sine wave
and inside there we will put our time
times time
sorry time to time times our limiter
frequency
times uh sorry the last one so that it
doesn't have a negative and positive
value
and uh we
multiply that by our limiter sound
that's our Max sound
and we also multiply that by our speed
ratio uh minus our actual uh limiter
engage value so that takes starts from
zero and goes to in this case 0.2
okay and now we add this to our running
sound pitch so rev limiter and let's see
how it sounds
okay let's see
[Music]
yeah it doesn't sound right
uh so uh
we want to change this here to instead
of going to our uh new running sound we
want this to go to our
to the address we want to reach so let's
we're gonna add this to our second
variable of our letter function and
let's see how that sounds
okay so that has a actual
oscillation in the sound
[Music]
so that sounds a lot more than a rev
limiter so we can actually uh
uh we can also increase the pitch if we
want to so uh the frequency we will add
this to uh
30 and let's change this to not 30 like
10 and let's send the limiter to three
let's see how that sounds
okay let's see
okay like a rev limiter to me although
it doesn't sound it does sound kinda
weird due to the dampening but
it's it's
so now next thing we want to do is
actually create a starting sound so if
our current hasn't started yet we want
it to start and then to actually run the
engine uh to do that we go to our car
controller and we create a new build
that we will call uh is engine running
but because we wanted to have three
states uh the not running State the
starting State and the running state
will made set this as int as an integer
and we will apply torque only if the
engine is running so we go to our apply
torque if engine is engine running is
higher than one so that's two went up
so that's the running State we actually
have our engine is running
and so we also want to create a function
in our audio which we'll call uh sorry
not a function at core routine so in
emerator
uh start thank you
which will set the running state of the
engine plus it will also
start the engine so we will go to our
ion emulator and uh we are start playing
the starting sound so we need to have a
new reference to our starting sound
okay and so we starting sound so we
start the starting sound well starts
that's play that's not a start
okay uh we set the engine our car
controller engine state
to one which is uh starting
we wait uh 0.6 seconds
yield return new weight for seconds
so now we also want to have a variable
here to
uh to check if our engine is running as
well
which will set false uh so
if engine is running
we actually change the sounds if it's
not
our idle of sound
that's right
will be equal to zero and our running
sound will also be equal to zero
and we enable now our uh is running
variable so we said that's true we also
now wait uh
around 0.4 seconds
so that we set our engine state to
running
so carcontroller dot is engine running
to to
okay
so now we also want a way to start the
engine so the way I'm gonna do it is
check if the gas pedal if the gas input
is higher if so if the user is pressing
the gas pedal so if
the absolute value of the gas input
is equal to so it's higher than zero uh
we want to find sorry to get the
component
a engine audio
and actually play the sound
start engine so start the engine
so that's a call routine so we started
with a start core routine function
okay but we also want this to only
launch once so we want to check that the
ease engine running is false
and then is equal is not equal to zero
let's see how that sounds
so we go back to Unity
uh we go to our starting sound and we
create a new sound
so here sorry forgot to name this to
running sound
so here uh we go to our audio source and
we set this to our starting sound
and we start and we also set the name to
the starting sound
we reference that to our script
and let's see how that works I hope that
works
okay that did not work
so I did not work because we have the
starting sound to play on awake so we
set that to zero we can we also need to
set all the values of our
our volumes to zero when we start so
like that and let's see now it works
okay so let's see if that works now
okay
when I press start it doesn't do
anything
uh that's because what okay that's
because uh in their car controller when
we actually check if the engine is
running we actually want to change that
it's not running and uh not that it is
running so let's see now
okay yes it's starting and it's now
and now the engine is running and when I
leave the gas pedal it stops rapping uh
yeah that's good so we also need to
create the reverse so but that's going
to be easy all right
so uh let's try it as well so for the
reverse we go to our ranging audio
we create we do the exact same thing we
did for our running sound but we
actually change it to reverse
so
um
okay so we go here and we want to check
whether our
our car is moving forward or if it's
going in the reverse so we will create a
new float sorry a new yeah in your float
uh which we will check if the speed
sorry the car is going forward backwards
so to do that we're going to create a
new variable to check if the engine so
if the wheels are moving forward
backwards so uh here that we set our
speed ratio
we're gonna actually get the absolute
value of that
and uh our we're gonna have a variable a
float which will be sign speed sign
uh it's all the if it's a 1 or -1
so speed sign will be equal to
the
sign of our variable
okay uh so now if the speed sign is
bigger than zero
so if uh what is is there a nerve here
so it's because we haven't initiated yep
so if that's higher than zero we're
gonna
set the running volume to a one sorry
not one but we're gonna set the running
volume
but the reverse volume will be equal to
zero
and we do the exact opposite thing of
that for our reverse so
here we copy paste that here
and instead of reverse sound being zero
we're gonna set the running sound
reverse sound reverse sound
reverse sound again reverse max volume
reverse sound
and uh not the reverse sound
so reverse Mark speeds sorry
okay and that should work
uh
uh yeah
so let's see
we set up the
car controller again to have the reverse
max volume to be around 0.2 and the mark
speeds to be one and we also create a
new
uh sound which will will contain a
reverse volume a reverse sound okay so
we put our range in Reverse here and uh
we reference it to our script
and let's see if that works
okay that's not working
as well because we also want this to be
equal to zero immediately
so let's see if it works okay so we if
the the engine starts we go forward
perfect we break we go backwards
and it sounds weird sounds really weird
so let's see why that is
okay so there are two problems with uh
okay so there are two problems with our
script first is that our car controller
here when it's when it checks for Speed
uh it tries to check for positive value
but we want absolute value so here
instead of speed we want the absolute
value of that so
the absolute value sorry of speed
we also want the absolute value of our
gas input so this should be
our absolute value
perfect
so let's see how that works now
so uh okay let's set it up to 0.2 and
let's see if that works
okay so let's start the car
we go backwards
and it still sounds weird
okay
and final thing we want to change is to
set the actual reverse volume to zero
instead of 0.3 so that it actually
sounds like reversing and we don't start
the engine immediately
so we go back to our car and let's see
if everything is set up correctly my
speed should be at 50. and let's set
this to 0.2 okay it's perfect let's say
so the car starts now
and reverse
perfect we break
and we accelerate perfect so let's
actually change this a little tiny bit
so that the reverse sounds a little bit
better so that should be around 0.35
and instead of the reverse bits being
0.3 to uh
0.2
let's see how that sounds now
so we start the engine we go forward
perfect
we break and we go backwards
perfect so yeah that's the sound system
I hope you liked it and thanks for
watching uh in the next episode I'll be
doing a gear system for uh the car so uh
the Rev revving sounds and uh gears
clutches Etc so yeah stay tuned for that
video and thanks for watching
浏览更多相关视频
Playing an Audio File - MetaSound Tutorials for Unreal Engine 5
【Unityゲーム作成講座2】アドベンチャーゲームの作り方#12(サウンドシステム①)【ゆっくり解説】
Cinematic SOUND DESIGN Tutorial for FILMMAKING
UEFN Progress Bar with Only a Material and Verse
How to Screen Record with Internal Audio on QuickTime Player
The SUPER Beginner Guide to Logic pro X
5.0 / 5 (0 votes)