HOW TO IMPLEMENT A BULLET WHIP SOUND MECHANIC
OUTLINE:
When trying to create an algorithm to check when the bullet “whip” sound should be played, the best solution is to rely on raw vector math. It should be played when the player is close enough to some part of the bullet’s trajectory after it has finished.
Tools:
Unity 2022.2.5f1, Visual Studio Community 2019 Version 16.11.8.
PLAN:
According to research, bullets will be fired with either a physical object or a RayCast. Normally, the implementation depends on which method is being used. The idea is to use a static function to check whether or not the sound should be played that can be used by both methods.
IMPLEMENTATION:
The following script (BulletLogic.cs) contains the core element of the algorithm for checking whether or not the bullet sound should be played:
This Function can then be called by a Bullet.cs script that is sitting on a physical bullet object:
If a RayCast method is being used for firing instead, then the BulletManager.ShouldPlaySound() function from this script can be used to pass in the start and the end of the RayCast as the variables instead:
Finally, the play logic of the sound will be implemented via a AudioManager script: