Security Cameras using C#

Janika Suhonen
3 min readFeb 29, 2024

--

Have you thought about creating a security camera mechanic for your game? Here is a simple way to do that!

What you need:

  • 3 Models for Security Cameras (Can be just simple boxes)
  • A Script that handles the security camera switches and main camera movement
  • UI elements (TextField and 2 Buttons)

Script

Namespaces and variables with comments

We will add the Empty GameObject to the Cameras array which is the parent for our mainCamera and the model. And we will turn that empty GameObject to make it look like that the camera model is turning.

You can tweak the rotation of that empty GameObject how you like.

The most important part is the curCamPosIndex which we use to determine which cam we are using currently.

In Start() function

  • Set the Main Camera to cam
  • Set the curCamPosIndex to 0, which is the first camera in the array
  • Move the cam to the cameras[curCamPosIndex] position according to which is the curCamPosIndex.
  • Changing the Text according to which camera is being used

In Update() function

We will have the turn mechanic for our camera if the player presses either Right Arrow or Left Arrow.

Rotating Camera

In the RotateRight() method we rotate the current camera parent with
Transform.Rotate

public void Rotate(Vector3 eulers, Space relativeTo = Space.Self);

eulers = The rotation to apply in euler angles.
Time.deltaTime =
We get the smooth rotation.
relativeTo =
The space where the rotation happens, it can be either locally around the object or globally.

RotateLeft() has only one change from RotateRight.
The Vector3 we use changes to -rotationVector which will change it to
(0,-10,0)

Changing Camera

When we change the camera, we have two buttons “<-” and “->”, these two will have different methods that they use.
We use the method below in the button which is “->”

We use this method in the button which has “->”

In the if statement we keep the index between 0–2 since there are only 3 cameras. 0, 1 and 2. So we can’t increment anymore since the integer is greater than 2.

When we set the parent to our camera, we use Transform.SetParent

We use this method in the button which has “<-”

This method is almost the same as the ChangeCameraRightButton(), except we check if the curCamPosIndex is greater than 0, if this is true, then we decrease the curCamPosIndex and update the cam position, parent and TextField.

--

--

Janika Suhonen
Janika Suhonen

Written by Janika Suhonen

From the beautiful snowy country with a touch of "good" humor? Inspired Unity Developer to learn more.

No responses yet