My journey becoming a Unity game developer: Security Cameras-Changing the color of the camera lights
Objective: Change the color of the cameras lights to red when the player is caught. Also, stop the camera from rotating when the player has been detected .
We want to turn the lights to a see through red color when the player is captured. To do this we need access to the Shader component and the Tint Color setting.
We will start by creating a new script name SecurityCamera. Create a new variable Type Game Object name gameOverCutscene which will hold the Game Over Cutscene game object. Use the OnTriggerEnter() function to find IF the tag equals the Player. If it does, we want to get the Mesh Renderer component within a new variable. Then, use that variable to assign it to the Color red. Finally, Set the gameOverCutscene variable to be Active.
The code above won’t work because the red color is too intense, and we need to change the Tint Color specifically.
Instead, after getting the Mesh Renderer component we will create a new variable Type Color name color. The values that will be passed in are (.7f, .2f, .2f, .05f) as a new Color which will change to the color red at half the intensity. Then, using the Mesh Renderer component variable Set the Color of the material using the _TintColor setting as a parameter and assigning the color variable’s values.
Now we have the lights turning red at half the intensity to allow the characters to be seen clearly.
There is also another way to change the color of the lights to red. Back in the Tint Color setting, we can use the 32-bit RGBA-255 color values.
For the color variable, we will use the Color32 Type instead of the regular Color Type. The new Color32 values will be (195, 44, 44, 11) which will still change the lights to red. Everything else in the code will stay the same.
Lights changes to red using the 32-bit format.
What we want to do next is stop the red light camera when the player is caught. Create another new variable Type Animator to be the handle of the Animator component name anim. After setting the color, disable the Animator attached to the Camera Cone game object using the anim variable. After disabling the Animator, start a coroutine to pause for half-second and activate the gameOverCutscene name AlertRoutine.
Select each Camera Cone game object and drag the actual camera game object with the animator we want to turn off into the Anim variable slot which will be Camera_1.
Now the camera will freeze with the red light turned on when the player is caught on camera.