How to Build an Interactive Security Camera System
Objective: Build a Security Camera System with Cinemachine.
To get started with building a Security Camera System, we will get the Starter Assets 3rd Person Controller from the Asset store.
Once added to your account, download from the webpage or from the Package Manager. This asset uses the new Input system, so it will ask if you want to enable it, select yes. Since I am not familar with the new Input System, I will enable the ability to use the old Input system. Do this by going to Edit → Project Settings. When the Window opens go to Player, scroll down till you see Active Input Handling. Set it to both.
After that is done go to Tools → Starter Assets → Reset Third Person Controller Armature.
We will get a Humanoid into the scene with a Camera now.
I went ahead and set up the scene to act like a hall way with a cube in the center.
Next we need to create three cameras. One with be a camera that scans the hallway back and forth, another is a static camera, and finally a POV camera with little movement. We’ll start with the scanning camera. To do this create a BlendlistCamera.
Move the Cameras to where you would like and add them into the Blend list.
Now our Camera rotates back and forth. Next is the Static Camera. Create a virtual camera and set it to where you would like.
Now finally the POV Camera. We will create another Virtual Camera and in the Aim set to POV. We will also limit how much one can rotate the camera. For vertical set the value range to “30 to 70” and for horizontal to “-90 to 0”. And disable Wrap.
Next is to set up a trigger script. Let’s create a Security Camera Script. This will need a OnTriggerEnter and OnTriggerExit methods. We will check for the player collider by using CompareTag.
Next we will need a variable for CinemachineVirtualCamera, int cameraSelect, bool for checking if in trigger, and CameraManager. I have covered this in this article.
I did change it from CinemachineVirtualCamera to CinemachineVirtualCameraBase.
In our OnTriggerEnter we will find the CameraManager, set our 3rdPersonCamera to false to disable it and set trigger bool to true.
In the OnTriggerExit we will reactive the 3rdPersonCamera and set the bool to false.
Now it’s time to cycle through the cameras. In our Update we will check for input on key C and if in the trigger. We will add one to the CameraSelect. Then we will check to see if it is greater or equal to the array in the Camera manager. If it is set back to zero. We will Reset the priority of all the cameras and set the next camera priority.
That finishes up this script. All that’s left is to assign the 3rd Person Camera onto the trigger script.
Now we are able to enter this trigger and see the different camera views.
The very last thing we should do is use the CinemachineBrain to control how the cameras blends with each other. Following yesterdays article on Cinemachine we will create a blend between each of the camera. Also all cameras back to the 3rd Person Camera.
That is all there is. Thank you for reading and have a wonderful day!