In this lesson, you will learn how to configure the Doppler effect to make your fast-moving projectiles and vehicles sound physically grounded as they zip past the player.
Understanding the Doppler Effect
The Doppler effect is the change in pitch of a sound caused by the relative motion between the source (like a speeding car) and the listener (the player). As an object moves toward the player, the sound waves compress, resulting in a higher pitch; as it moves away, the waves stretch, resulting in a lower pitch. In Unity, the AudioSource component handles this automatically, but only if you configure the global physics settings and the source’s properties correctly.
Configuring Your Audio Sources
For the Doppler effect to trigger, Unity needs to know how "fast" your objects are moving in the world. First, navigate to Edit > Project Settings > Audio. Here, you will find the Doppler Factor. Setting this to 1.0 is standard for realistic physics; setting it higher will exaggerate the pitch shift, which is often desirable for arcade-style fast-paced games.
Next, ensure your AudioSource on your projectile or vehicle is set up properly:
- Spatial Blend: Ensure this is set to
3D(pushed to the right). The Doppler effect only works on 3D spatialized sounds. - Velocity: Unity’s
AudioSourcecalculates the Doppler shift based on the movement of theGameObjectit is attached to. Ensure your projectiles are moving usingRigidbodyphysics or by updating theirtransform.positionevery frame.
Real-world Application
If your projectile is a simple script-moved object, ensure it is assigned a Rigidbody. If you are moving it purely via code, Unity may struggle to calculate the velocity needed for the pitch shift. You can manually push the velocity to the AudioSource if necessary:
void Update() {
// Manually updating velocity if not using Rigidbody physics
audioSource.velocity = velocityVector;
}
By ensuring your AudioSource has a 3D spatial blend and that your objects are translating through space, Unity will automatically handle the frequency shift, adding a crucial layer of immersion to your high-speed gameplay.
Key Takeaway: To achieve a realistic Doppler effect, ensure your AudioSource is in 3D mode and verify that the object's velocity is accurately tracked by the physics engine.