Matching audio pitch to animation speed

Many artifacts sound higher in pitch when accelerated and lower when slowed down. Car engines, fan coolers, a Vinyl record player... the list goes on. If you want to simulate this kind of sound effect in an animated object that can have its speed changed dynamically, follow this recipe.

Getting ready

For this recipe, you'll need an animated 3D object and an audio clip. Please use the carousel.fbx and carouselSound.wav files, available in the 0423_06_01 folder.

How to do it...

To change the pitch of an audio clip according to the speed of an animated object, please follow these steps:

  1. Import the carousel.fbx file into your Unity project.
  2. Select the carousel.fbx file in the Project view. Then, in the Inspector view, check its Import Settings. Under Animations, select the Take 001 clip and make sure to check the Loop Pose option. Click the Apply button to save changes:
    How to do it...
  3. Add the carousel to the scene by dragging it from the Project view into the Hierarchy view.
  4. Add a Directional Light to the scene through the Create drop-down menu on top of the Hierarchy view.
  5. Import the carouselSound.wav audio clip file.
  6. Select the carousel game object and drag carouselSound from the Project view into the Inspector view, adding it as an audio source for that object.
  7. In the Audio Source component of the carousel, check the box for the Loop option:
    How to do it...
  8. We need to create a controller for our object. In the Project view, click the Create button and select Animator Controller. Name it CarouselController.
  9. Double click CarouselController to open the Animator view. Then, right-click the gridded area and select Create State | Empty from the contextual menu:
    How to do it...
  10. Name the new state spin and set Take 001 as its motion in the Motion field:
    How to do it...
  11. From the Hierarchy view, select the carousel. Then, in the Animator component (in the Inspector view), set CarouselAnimator as its Controller and uncheck the Apply Root Motion option:
    How to do it...
  12. In the Project window, create a new C# script and rename it as ChangePitch.
  13. Open the script in your editor and replace everything with the following code:
    using UnityEngine;
    
    public class ChangePitch : MonoBehaviour{
       public float speed = 0.0f;
       public float minSpeed = 0.0f;
       public float maxSpeed = 2.0f;
       public float animationSoundRatio = 1.0f;
       private Animator animator;
     void Start(){
         animator =  GetComponent<Animator>();
     } 
        void Update(){
            animator.speed = speed;
            audio.pitch = speed * animationSoundRatio;
        }
        void OnGUI(){
            Rect rect = new Rect(10, 10, 100, 30);
            speed = GUI.HorizontalSlider(rect, speed, minSpeed, maxSpeed);
        }
    }
  14. Save your script and add it as a component to the carousel.
  15. Play the scene and change the animation speed, along with the audio pitch, using the slidebar.

How it works...

The idea behind the script and its implementation are actually quite straightforward. It creates a slidebar from which the user can change the speed of the animator component. Then, it updates the audio pitch based on that number.

There's more...

Here is some information on how to fine-tune and customize this recipe.

Changing the Animation / Sound Ratio parameter

If you want the audio clip pitch to be either more or less affected by the animation speed, change the value of the Animation / Sound Ratio parameter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.163.250