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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.