Unity 2018 Cookbook(Third Edition)
上QQ阅读APP看书,第一时间看更新

Allowing reverse animation (negative speeds!)

In the Animator panel, create a new Float parameter, Speed, initialized to 1.0. With state spin selected in the Animator panel, check the Speed Parameter Multiplier option and choose the Speed parameter. In the Inspector, set Min Speed to -2, to allow negative speeds for animations.

In the ChangePitch C# script, replace the AccelerateRocket method with the following:

public void AccelerateRocket(float acceleration) { 
   speed += acceleration;  
   speed = Mathf.Clamp(speed, minSpeed, maxSpeed); 
 
   animator.SetFloat("Speed", speed); 
   float soundPitch = speed * animationSoundRatio; 
   audioSource.pitch = Mathf.Abs(soundPitch); 
} 

Now when you use key 1 (accelerate) and 2 (decelerate), you can actually decelerate to zero and then continue to reverse the animation.