Car controller tutorial #2: Car Engine Audio(realistic) in Unity + GitHub link

Nanousis Development
8 Dec 202227:43

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

00:00

๐Ÿ”Š 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.

05:02

๐Ÿ› ๏ธ 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.

10:03

๐Ÿ”ง 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.

15:04

๐ŸŽต 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.

20:06

๐Ÿš— 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.

25:08

๐Ÿ” 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

A sound system in the context of the video refers to the audio components and processes designed to produce realistic engine sounds for a car in a video game. The video's theme revolves around creating a dynamic audio experience that includes starting, running, and reverse sounds, enhancing the car game's realism and immersion.

๐Ÿ’กAudio Source

An audio source is a component in game development used to attach sound files to objects within the game environment. In the script, audio sources are assigned to different sounds such as 'running sound' and 'idle sound' to simulate the car's engine noises at various states.

๐Ÿ’กLooping

Looping, in audio terms, means a sound that repeats continuously. The video script mentions ensuring that the engine sounds are set to loop, which is crucial for creating a continuous engine noise without abrupt stops, thus maintaining realism while the car is in motion.

๐Ÿ’กScript

In video game development, a script often refers to a piece of code that defines how certain game elements behave. The video discusses creating a script called 'engine audio' to control the audio sources and manage the car's sound dynamics based on its speed and user input.

๐Ÿ’กCar Controller

The car controller is a script or system within the game that manages the car's movement and interactions. The script references the car controller to obtain the vehicle's speed and gas pedal input, which are then used to modulate the engine sounds accordingly.

๐Ÿ’กSpeed Ratio

Speed ratio is a calculated value that represents the proportion of the car's current speed relative to its maximum speed. The script describes using the speed ratio to adjust the engine sound's volume and pitch, creating a more dynamic and realistic audio response to the car's performance.

๐Ÿ’กRev Limiter

A rev limiter is a mechanism that prevents an engine from exceeding a certain RPM (revolutions per minute) to protect the engine from damage. In the video, a simulated rev limiter effect is added to the sound system to create an oscillating pitch that mimics the engine's behavior when it reaches its maximum RPM.

๐Ÿ’กStarting Sound

The starting sound refers to the specific audio clip that plays when the car's engine is initiated. The script details creating a starting sound effect that plays once when the engine starts, contributing to the overall realism of the car's operation.

๐Ÿ’กReverse Sound

Reverse sound is the audio cue that is played when the car is put into reverse gear. The script explains setting up a reverse sound that plays when the car moves backward, further enhancing the game's audio feedback to the player's actions.

๐Ÿ’กMax Speed

Max speed is the highest speed that the car can achieve. In the script, max speed is used as a reference to limit the car's speed and to calculate the speed ratio, which is essential for adjusting the engine sound based on the car's performance.

๐Ÿ’กPitch

Pitch in music and sound design refers to the perceived frequency of a sound, which determines how high or low it sounds. The script discusses changing the pitch of the engine sound to reflect changes in engine speed, such as when the car is accelerating or at the rev limit.

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

play00:00

so in this video I'll be showing you how

play00:02

to create a sound system for your car so

play00:05

an engine sound system without gears for

play00:09

your car game that will have a starting

play00:11

sound running sound and reverse sound so

play00:14

let's see how it sounds so we start the

play00:17

engine and we go forward

play00:19

[Music]

play00:21

okay we leave The Gas We press the gas

play00:23

again

play00:25

now we stop it we stop the car and we Go

play00:28

reverse

play00:31

and now that's the reverse sound

play00:33

so forward

play00:36

with a red lifter

play00:39

with stop

play00:41

and we can also go reverse

play00:44

so yeah that's it okay so first thing we

play00:47

want to do is create an audio source for

play00:49

our engine to do that we go to our car

play00:51

and we create a new empty object which

play00:54

we will call running sound we put that

play00:57

around the engine or place close to it

play01:00

and we also add an audio source to it

play01:03

now we duplicate that so that we have

play01:05

our idle sound as well and now we want

play01:09

to put our audio of Clips to our sources

play01:12

so the running sound should go to our

play01:15

audio

play01:16

for running sound the idle should go to

play01:19

our idle

play01:20

we also

play01:23

we also want to make sure that uh our

play01:26

sounds are looping so we need to click

play01:28

this Loop here

play01:29

and our running sound to loop as well

play01:32

okay so now what we do is create a new

play01:36

script in our car which we will call uh

play01:39

audio no engine audio

play01:46

okay you can see the script that's all

play01:51

yep so here's the script for now

play01:55

and now we can open it

play01:57

okay so now we want to reference our two

play02:00

audio files I have sources so we go to

play02:03

our hidden script and call a public

play02:05

audio Source this will go running sound

play02:10

we also want to have a max volume so we

play02:13

will have a float that we will call Max

play02:16

sorry running Max sound

play02:19

max volume

play02:24

and since we also want to have a new hey

play02:27

so we want to change the pitch of the

play02:30

engine as well we won't have a running

play02:32

Max Beach

play02:35

and we do that exact same thing for uh

play02:38

our idle as well let me change the names

play02:42

a little bit

play02:43

okay

play02:47

okay so we did that for arrival as well

play02:49

we just change the names

play02:51

I'll sound

play02:55

idle running

play02:58

all right so what we want to do now is

play03:01

uh make sure that whenever our car is

play03:04

actually moving forward uh and the cast

play03:07

pedal is pressing that will actually

play03:08

produce a sound so to do that we also

play03:11

want to reference our

play03:13

car controller so we will reference it

play03:17

here

play03:19

and we will also get this from the get

play03:22

component script in unity so

play03:25

when started to get the car controller

play03:27

okay so now we want to actually get the

play03:30

speed of the vehicle and uh how much gas

play03:32

pedal is present so uh before we do

play03:36

anything with the audio we want to go to

play03:39

our uh

play03:41

car controller and get the actual speed

play03:43

of the vehicle oh sorry not the speed of

play03:46

the vehicle but actually the speed of

play03:48

the wheels that are spinning since we

play03:49

don't have a gear system yet we will get

play03:51

the speed of the wheels and we will

play03:54

convert it to engine sound

play03:56

and to do that we go to our car

play03:58

controller

play03:59

and we go to our speed a variable and we

play04:03

change it that instead we that we use

play04:05

the velocity we use the

play04:09

wheel RPM times its radius so sorry

play04:12

times its circumference

play04:14

so that we get the actual wheel speed

play04:16

okay so to do that we call the colliders

play04:19

of our wheels so we get our rear right

play04:22

wheel we can't use any wheel or the

play04:24

average of them times the RPM

play04:26

times the radius of the wheels so

play04:31

that's radius times two times

play04:34

uh Pi which is uh the formula for this

play04:38

conference and since we don't want this

play04:39

to be a huge uh number we will divide it

play04:43

by 10.

play04:44

okay so now that we have our speed we

play04:46

also want to know the max speed of the

play04:48

vehicle so we will create a new variable

play04:51

in our car which we will call public

play04:53

float a max speed

play04:55

and we will also limit the car's speed

play04:59

to that speed so uh if the speed is

play05:01

higher than the than our actual speed

play05:03

sorry it's an actual Max Speed then we

play05:06

will not apply any motor torque so we go

play05:09

to our apply motor and we say if the

play05:14

speed

play05:15

is uh higher than max speed

play05:19

so if it's lower than our Max Speed then

play05:21

we can actually apply some motor torque

play05:23

if it's not

play05:25

then we the motor torque is zero

play05:29

so we take this

play05:31

and we actually say that's zero

play05:37

okay

play05:39

and now we want to also create a

play05:41

function that actually gives the ratio

play05:44

of the gas pedal times the speed divided

play05:47

by our Max Speed so that we get zero to

play05:50

one value of uh how much engine are we

play05:53

using all right so we create a new

play05:56

public function which will return float

play06:00

and uh it will be called the get speed

play06:03

ratio

play06:05

and so we want to actually get our gas

play06:08

input as well so that's going to be uh

play06:11

the clamp of the

play06:14

gas pedal from 0.5

play06:17

to 1.

play06:20

and we're gonna also give it our gas

play06:21

input

play06:22

so that actually emits our gas to be

play06:25

from 0.5 to 1 so it's actually uh half

play06:28

of the gas input and now uh we want to

play06:33

actually

play06:34

return

play06:36

our speed times our gas divided by our

play06:40

max speed

play06:41

perfect so now we go back to our audio

play06:43

source and we actually change the sound

play06:47

of the audio to monster speed okay so uh

play06:51

now we want to actually check what the

play06:53

ratio is so we create a new float which

play06:56

we will call uh speed ratio

play07:01

and now we want to reference that speed

play07:03

ratio so we check that our car

play07:06

controller is actually there

play07:07

and if it is uh the speed ratio will be

play07:10

equal to the

play07:13

speed that we have so the

play07:16

car controller dot get Speedways here

play07:20

perfect so now we actually change the

play07:23

volume of the sounds so we will set the

play07:26

alt the idle sound the volume to be

play07:29

equal to

play07:30

uh the

play07:34

the in between of

play07:36

1.0.1 F and our actual uh

play07:40

say idle Max sound

play07:43

and the between value will be our actual

play07:47

speed so

play07:49

that's that

play07:51

also we want to have our running sound

play07:55

to be equal to our speed times our gas

play07:58

which we that's basically our speed

play07:59

ratio and if we Redline our car so we

play08:02

are at maximum engine speed to actually

play08:04

uh

play08:07

move the pitch back and forth so that it

play08:10

actually sounds like it's cutting of

play08:12

power and it's giving power it's cutting

play08:13

of power it's giving power as well like

play08:15

a regular rev limiter will do so to do

play08:19

that uh we actually want to actually

play08:23

change our running

play08:25

uh sound volume to be equal to the in

play08:29

between of uh 0.3 F and our

play08:33

Max ironic sound

play08:36

and the sound will be also equal to our

play08:40

speed ratio

play08:41

and now the pitch

play08:42

will be equal

play08:45

to our uh in between again uh

play08:49

how far uh

play08:51

running pitch

play08:53

sorry over 0.3 F again and our running

play08:58

Mac speech

play09:00

and that's going to be equal to our

play09:02

speed ratio times the

play09:05

sorry yeah no actually times the actual

play09:08

uh speed ratio

play09:10

and let's try that and next we'll do the

play09:12

rev limiter

play09:13

let's see if that works

play09:17

let's set up everything uh running sound

play09:21

idle sound

play09:24

and also we want to set our max volume

play09:26

to 1 our Max speeds to two again

play09:30

also we don't need the max speed for

play09:31

idle since we are not going to change

play09:33

the idle pitch so that's going to delete

play09:36

it

play09:37

and let's see if it works

play09:40

okay let's see if it works now we will

play09:42

put the max speed here to 50. and so

play09:46

let's check if it works

play09:48

okay so we can definitely hear the

play09:50

suspensions out right now

play09:52

and if we press the gas

play09:55

we can actually hear that

play09:58

but the problem here is uh that if you

play10:01

can hear there is this cracking nose is

play10:03

when the car is reaching its top speed

play10:08

okay and obviously we don't want that

play10:10

so to fix that we want to clamp the

play10:13

sound so when it cuts off the engine

play10:14

power it basically emulates however rev

play10:17

limiter works and it doesn't just cut

play10:19

off the actual uh sound of the engine

play10:24

alright so to do that we need to go back

play10:26

to our power controller and change the

play10:29

way we use the speed so we create a new

play10:31

variable which we will call speed

play10:33

clamped

play10:34

so uh private sorry

play10:39

load speed clubs

play10:42

and we go back to our update and we

play10:45

create we set this speed clamp variable

play10:48

to be equal to the massive.lurk DOT

play10:51

speedclamp

play10:55

sorry

play10:57

uh two

play10:59

uh actual value of our speed

play11:03

so to this so speed

play11:05

and it's time to Delta time

play11:09

which means how fast the frame took and

play11:12

we actually use that variable instead of

play11:14

our speed for our get speed ratio

play11:18

okay we can also go to our engine audio

play11:20

and instead of changing immediately the

play11:23

pitch we can change it to its actual

play11:26

self so it's also dampened so we create

play11:28

a new mapf.herb we call it sorry the

play11:32

first variable will be our Mac our

play11:34

running sound Dot Peach

play11:37

our second will be our actual page that

play11:39

we want to achieve and finally we will

play11:41

also use our time.delt attack

play11:44

and let's see if this works

play11:48

okay let's see

play11:50

so we break the gas

play11:54

it's accelerating

play11:55

[Music]

play11:58

reaching a limit where uh once you reach

play12:02

this limit it doesn't go any further so

play12:05

the pitch doesn't change any further so

play12:07

it doesn't create a weird sound so now

play12:09

what we want to do is also create the

play12:11

rev limiter sound to do that we go back

play12:13

to arranging audio and we create three

play12:15

variables which we will call rev limiter

play12:18

max volume frequency

play12:21

yeah I think that's two and uh let's see

play12:24

what else we will create

play12:26

and uh at what variable the rev limiter

play12:30

engages

play12:31

so that's going to be public float

play12:36

a limiter

play12:39

a sound which we will set to one f for

play12:43

now uh

play12:45

the limiter frequency

play12:48

the frequency that it oscillates

play12:51

which we will set to 3F for now and uh

play12:55

the public

play12:57

load limiter engage

play12:59

which is going to be 0.8 F which means

play13:02

that if our speed ratio is 0.8 F and

play13:05

higher the revenue limiter will engage

play13:08

sorry if the Rev If the ratio

play13:11

is higher than 0.85 sorry that our

play13:14

limiter engage

play13:16

let's make fix this typo

play13:19

then our Revenue limiter

play13:22

will be so we also need to have a rev

play13:25

limiter loadable

play13:30

okay so our rev limiter

play13:32

will be equal to our math f

play13:35

DOT sign which will be our uh which is

play13:38

the basically the sign uh

play13:41

function uh that oscillates up and down

play13:44

like a sine wave

play13:46

and inside there we will put our time

play13:48

times time

play13:50

sorry time to time times our limiter

play13:53

frequency

play13:55

times uh sorry the last one so that it

play13:57

doesn't have a negative and positive

play13:59

value

play14:00

and uh we

play14:02

multiply that by our limiter sound

play14:06

that's our Max sound

play14:08

and we also multiply that by our speed

play14:10

ratio uh minus our actual uh limiter

play14:13

engage value so that takes starts from

play14:16

zero and goes to in this case 0.2

play14:21

okay and now we add this to our running

play14:25

sound pitch so rev limiter and let's see

play14:28

how it sounds

play14:34

okay let's see

play14:36

[Music]

play14:43

yeah it doesn't sound right

play14:46

uh so uh

play14:48

we want to change this here to instead

play14:52

of going to our uh new running sound we

play14:56

want this to go to our

play15:00

to the address we want to reach so let's

play15:03

we're gonna add this to our second

play15:05

variable of our letter function and

play15:07

let's see how that sounds

play15:15

okay so that has a actual

play15:19

oscillation in the sound

play15:21

[Music]

play15:24

so that sounds a lot more than a rev

play15:26

limiter so we can actually uh

play15:31

uh we can also increase the pitch if we

play15:34

want to so uh the frequency we will add

play15:37

this to uh

play15:39

30 and let's change this to not 30 like

play15:42

10 and let's send the limiter to three

play15:44

let's see how that sounds

play15:47

okay let's see

play15:53

okay like a rev limiter to me although

play15:55

it doesn't sound it does sound kinda

play15:57

weird due to the dampening but

play16:00

it's it's

play16:01

so now next thing we want to do is

play16:03

actually create a starting sound so if

play16:05

our current hasn't started yet we want

play16:07

it to start and then to actually run the

play16:09

engine uh to do that we go to our car

play16:11

controller and we create a new build

play16:14

that we will call uh is engine running

play16:19

but because we wanted to have three

play16:21

states uh the not running State the

play16:25

starting State and the running state

play16:27

will made set this as int as an integer

play16:31

and we will apply torque only if the

play16:34

engine is running so we go to our apply

play16:36

torque if engine is engine running is

play16:39

higher than one so that's two went up

play16:42

so that's the running State we actually

play16:44

have our engine is running

play16:47

and so we also want to create a function

play16:50

in our audio which we'll call uh sorry

play16:54

not a function at core routine so in

play16:55

emerator

play16:59

uh start thank you

play17:02

which will set the running state of the

play17:05

engine plus it will also

play17:08

start the engine so we will go to our

play17:11

ion emulator and uh we are start playing

play17:15

the starting sound so we need to have a

play17:18

new reference to our starting sound

play17:24

okay and so we starting sound so we

play17:29

start the starting sound well starts

play17:31

that's play that's not a start

play17:34

okay uh we set the engine our car

play17:38

controller engine state

play17:40

to one which is uh starting

play17:44

we wait uh 0.6 seconds

play17:48

yield return new weight for seconds

play17:53

so now we also want to have a variable

play17:56

here to

play17:59

uh to check if our engine is running as

play18:01

well

play18:02

which will set false uh so

play18:07

if engine is running

play18:11

we actually change the sounds if it's

play18:14

not

play18:17

our idle of sound

play18:20

that's right

play18:23

will be equal to zero and our running

play18:25

sound will also be equal to zero

play18:28

and we enable now our uh is running

play18:31

variable so we said that's true we also

play18:35

now wait uh

play18:39

around 0.4 seconds

play18:42

so that we set our engine state to

play18:45

running

play18:47

so carcontroller dot is engine running

play18:50

to to

play18:51

okay

play18:53

so now we also want a way to start the

play18:55

engine so the way I'm gonna do it is

play18:58

check if the gas pedal if the gas input

play19:00

is higher if so if the user is pressing

play19:03

the gas pedal so if

play19:06

the absolute value of the gas input

play19:09

is equal to so it's higher than zero uh

play19:14

we want to find sorry to get the

play19:17

component

play19:18

a engine audio

play19:21

and actually play the sound

play19:23

start engine so start the engine

play19:26

so that's a call routine so we started

play19:28

with a start core routine function

play19:36

okay but we also want this to only

play19:39

launch once so we want to check that the

play19:42

ease engine running is false

play19:46

and then is equal is not equal to zero

play19:50

let's see how that sounds

play19:52

so we go back to Unity

play19:55

uh we go to our starting sound and we

play19:58

create a new sound

play20:00

so here sorry forgot to name this to

play20:03

running sound

play20:05

so here uh we go to our audio source and

play20:10

we set this to our starting sound

play20:13

and we start and we also set the name to

play20:16

the starting sound

play20:18

we reference that to our script

play20:21

and let's see how that works I hope that

play20:23

works

play20:26

okay that did not work

play20:30

so I did not work because we have the

play20:32

starting sound to play on awake so we

play20:35

set that to zero we can we also need to

play20:37

set all the values of our

play20:41

our volumes to zero when we start so

play20:45

like that and let's see now it works

play20:49

okay so let's see if that works now

play20:53

okay

play20:55

when I press start it doesn't do

play20:57

anything

play20:58

uh that's because what okay that's

play21:01

because uh in their car controller when

play21:03

we actually check if the engine is

play21:05

running we actually want to change that

play21:06

it's not running and uh not that it is

play21:09

running so let's see now

play21:13

okay yes it's starting and it's now

play21:17

and now the engine is running and when I

play21:20

leave the gas pedal it stops rapping uh

play21:24

yeah that's good so we also need to

play21:27

create the reverse so but that's going

play21:29

to be easy all right

play21:31

so uh let's try it as well so for the

play21:34

reverse we go to our ranging audio

play21:36

we create we do the exact same thing we

play21:40

did for our running sound but we

play21:43

actually change it to reverse

play21:45

so

play21:46

um

play21:48

okay so we go here and we want to check

play21:51

whether our

play21:53

our car is moving forward or if it's

play21:55

going in the reverse so we will create a

play21:58

new float sorry a new yeah in your float

play22:01

uh which we will check if the speed

play22:04

sorry the car is going forward backwards

play22:09

so to do that we're going to create a

play22:11

new variable to check if the engine so

play22:13

if the wheels are moving forward

play22:14

backwards so uh here that we set our

play22:17

speed ratio

play22:18

we're gonna actually get the absolute

play22:20

value of that

play22:28

and uh our we're gonna have a variable a

play22:31

float which will be sign speed sign

play22:34

uh it's all the if it's a 1 or -1

play22:40

so speed sign will be equal to

play22:43

the

play22:45

sign of our variable

play22:54

okay uh so now if the speed sign is

play22:58

bigger than zero

play23:04

so if uh what is is there a nerve here

play23:12

so it's because we haven't initiated yep

play23:14

so if that's higher than zero we're

play23:17

gonna

play23:20

set the running volume to a one sorry

play23:25

not one but we're gonna set the running

play23:27

volume

play23:28

but the reverse volume will be equal to

play23:30

zero

play23:32

and we do the exact opposite thing of

play23:35

that for our reverse so

play23:38

here we copy paste that here

play23:41

and instead of reverse sound being zero

play23:44

we're gonna set the running sound

play23:45

reverse sound reverse sound

play23:48

reverse sound again reverse max volume

play23:53

reverse sound

play23:55

and uh not the reverse sound

play23:59

so reverse Mark speeds sorry

play24:01

okay and that should work

play24:03

uh

play24:04

uh yeah

play24:06

so let's see

play24:09

we set up the

play24:12

car controller again to have the reverse

play24:16

max volume to be around 0.2 and the mark

play24:19

speeds to be one and we also create a

play24:22

new

play24:23

uh sound which will will contain a

play24:26

reverse volume a reverse sound okay so

play24:30

we put our range in Reverse here and uh

play24:33

we reference it to our script

play24:36

and let's see if that works

play24:39

okay that's not working

play24:42

as well because we also want this to be

play24:45

equal to zero immediately

play24:54

so let's see if it works okay so we if

play24:58

the the engine starts we go forward

play25:01

perfect we break we go backwards

play25:05

and it sounds weird sounds really weird

play25:08

so let's see why that is

play25:10

okay so there are two problems with uh

play25:14

okay so there are two problems with our

play25:16

script first is that our car controller

play25:18

here when it's when it checks for Speed

play25:21

uh it tries to check for positive value

play25:24

but we want absolute value so here

play25:26

instead of speed we want the absolute

play25:29

value of that so

play25:31

the absolute value sorry of speed

play25:35

we also want the absolute value of our

play25:38

gas input so this should be

play25:42

our absolute value

play25:45

perfect

play25:47

so let's see how that works now

play25:53

so uh okay let's set it up to 0.2 and

play25:56

let's see if that works

play25:59

okay so let's start the car

play26:02

we go backwards

play26:07

and it still sounds weird

play26:13

okay

play26:14

and final thing we want to change is to

play26:16

set the actual reverse volume to zero

play26:19

instead of 0.3 so that it actually

play26:23

sounds like reversing and we don't start

play26:26

the engine immediately

play26:27

so we go back to our car and let's see

play26:29

if everything is set up correctly my

play26:31

speed should be at 50. and let's set

play26:34

this to 0.2 okay it's perfect let's say

play26:37

so the car starts now

play26:39

and reverse

play26:42

perfect we break

play26:45

and we accelerate perfect so let's

play26:48

actually change this a little tiny bit

play26:50

so that the reverse sounds a little bit

play26:53

better so that should be around 0.35

play26:57

and instead of the reverse bits being

play27:00

0.3 to uh

play27:02

0.2

play27:05

let's see how that sounds now

play27:08

so we start the engine we go forward

play27:14

perfect

play27:15

we break and we go backwards

play27:22

perfect so yeah that's the sound system

play27:25

I hope you liked it and thanks for

play27:28

watching uh in the next episode I'll be

play27:30

doing a gear system for uh the car so uh

play27:34

the Rev revving sounds and uh gears

play27:37

clutches Etc so yeah stay tuned for that

play27:41

video and thanks for watching

Rate This
โ˜…
โ˜…
โ˜…
โ˜…
โ˜…

5.0 / 5 (0 votes)

Related Tags
Car SoundGame AudioEngine SystemUnity ScriptingSound DesignGaming TutorialAudio SourceEngine EffectsRev LimiterSound System