How it works...
You created a C# script class, ChangePitch, and added an instance to the animatedRocket GameObject. It declares several variables, the most important of which is acceleration.
It's Awake() method caches references to the Animator and AudioSource sibling components. The Start() method sets the initial speed from the Animator, and calls the AccelerateRocket(...) method, passing 0 to calculate the resulting pitch for the Audio Source.
In each frame, the Update() method tests for keyboard keys 1 and 2. When detected, they call the AccelerateRocket(...) method, passing a positive or negative value of acceleration as appropriate.
The AccelerateRocket(...) method increments variable speed with the received argument. The Mathf.Clamp() ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access