How to do it...

To animate public script variables, follow these steps:

  1. Create a new script and call it FogAnimator.cs.
  2. Create two public variables in this script: public float fogDensity and public Color fogColor.
  3. In this script's Update() function, we call the ChangeFog Trigger in the controller when player presses the space bar. We also set the RenderSettings.fogColor and RenderSettings.fogDensity parameters using our public variables. We also adjust the Main Camera's background color to match the fog color:
        if (Input.GetKeyDown(KeyCode.Space)) 
        { 
            anim.SetTrigger("ChangeFog"); 
        } 
        RenderSettings.fogColor = fogColor; 
        RenderSettings.fogDensity = fogDensity; 
 
        Camera.main.backgroundColor = fogColor; 
  1. Create a new game object and name it 

Get Unity 5.x Animation Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.