Day 79: Creating Security Cameras in Unity

Tyler Smallwood
5 min readAug 23, 2021

Objective: Create logic for the security cameras to catch the player!

Today is all about setting up cameras to catch the player. The first step will be setting up the cameras to rotate. Then we will cover how to check to see if the player is in the area of the cone. Lastly how to turn the cone red and start the game over scene.

We will need to select the camera and create an animation. We will rotate our camera from 54 to -54 on the Y axis to get the rotation effect.

We have the camera going one way and not smoothly rotating back. We’ll do this in a way to get it to have a nice transition back and forth. With the camera selected we need to go to the animator window.

With the animator window you will have only one state in there after creating your animation.

You will need to copy that state and paste it in and rename it something to refer it going backwards. Mine is named Reverse.

To have the Reverse one go in the opposite direction as the original state, with it selected we need to look at the inspector. There is a speed variable that only goes from -1 to 1. Normally states will be selected to 1 and it’ll more like I’ve shown above. We put it to -1 and the animation will move backwards.

After setting up the animation with a speed of -1, we need to make transitions from the regular animation to the reverse animation. In this case we don’t need to set anything up for the transitions to activate. Just letting them run their course and they will move to the next animation in line.

In play mode this is how it’ll look.

Now to catch the player. Under the Camera there is a game object for the green cone. On that object we will create a new script called SecurityCameras.

Inside this script we will need to have a OnTriggerEnter function. And like we have done in the pass we will use “other.CompareTage(“Player”)” to see if it was the player that entered the trigger.

We will start with changing the color. After we make sure it was the player that entered the cone, we will get a reference to the render by using GetComponent. Next we will create a new Color by creating one using a Constructor from the Color class. The values below are the final product I was able to come up with. After that we will use the the render to set the color of the Tint.

To find the Variable name of Tint Color, on the material we will hit the gear icon and click Edit Shader.

A window will pop open and the variable name will be there.

After setting the color we will need to stop the animation of the camera that caught the player. For the cameras we create a variable and just dragged it into the script. We did it this way because the object that holds the script is a child. To do this just set the animation to false.

To finish up the camera we want to start the game over cutscene. But we do not want it to start right away or we wont see the camera turn red. To do this we will need to create a Coroutine to slow it down a little.

After the yield return we just set the cutscene object to true. The same way we got the animator is how we’ll get the cutscene, just drag it in to the script.

After that it’s done. Our security cameras will turn red when a player enters the cone and start the cutscene of being caught.

Thank you for reading and have a wonderful day!

--

--

Tyler Smallwood

I am passonate on learning to program and use Unity to become a skillful Unity Developer