SanAndreasUnity
SanAndreasUnity copied to clipboard
Car engine sound
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;
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.
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.