SanAndreasUnity icon indicating copy to clipboard operation
SanAndreasUnity copied to clipboard

Car engine sound

Open in0finite opened this issue 4 years ago • 2 comments

Implement car engine sound.

Sound loading already works, use AudioManager.CreateAudioClipFromSfx() to load sounds.

When vehicle is created (eg. in Vehicle.Start()), load it's engine sound. Make sure to cache loaded sounds (eg. in a Dictionary), so we don't have to load them every time.

Each group of vehicles has different sound, so they need to be identified first.

We need to determine current RPM of the engine, so that we can increase or decrease pitch of engine sound. RPM can be determined based on current velocity, maximum velocity and number of gears. Something like this:

float velocityInGear = currentVelocity % (maxVelocity / numGears);
float rpmPercentage = velocityInGear / (maxVelocity / numGears);
audioSource.pitch = rpmPercentage;

in0finite avatar Aug 01 '21 17:08 in0finite

Your script is not valid, because each gear must have it's ratio (the first gear is short while the fifth is long). Actually this script works but the gears are splitted equally along the max velocity.

LDami avatar Aug 08 '21 00:08 LDami

Well, yeah you are probably right. Although the ratios are almost equal I would say, so their differences can be ignored, at least for a start.

in0finite avatar Aug 08 '21 00:08 in0finite