3D Survival FPS game

Creating Camera movement

Simon Truong
4 min readMar 21, 2022

--

Now that we have gotten our player movement, it is time to create the heart and soul of any FPS game, the camera movement (aka Aiming).

Working with the Main Camera:

Before we can work with the scripting, the first thing we can do to simulate how the camera movement function will work is simply dragging the main camera into the Player as a child object.

By doing now, you will automatically see how the camera now follows the player from a set location. Unfortunately due action is extremely limited because as you can see, the camera abruptly shifts when changing directions.

Scripting the camera to work with the mouse:

To get started with scripting the camera, we are going to create a new C# script that will be named “Camera Movement”. To understand what we are about to do, we are basically converting the camera rotation to the mouse input. Just like how we create our player movement, we also have access to mouse movement/position as well, namely called “ Mouse X” and “Mouse Y”.

In the Camera Movement script we are first going to need a global variable for the Main Camera. Then we are going to assign the main camera to the script, since in Unity there is only ever one Main Camera in each instance, we can basically write “camera.main”.

I added a null check just to be proper.

Then we are going to create a new method called “Mouse Movement”, this method is going to house all the mouse to camera function.

The first thing I want to to within this movement method is create two local variables that assign the input system to a name.

To make the camera move right and left we need to assign a new Vector3 variable called “current Rotation” that will equal the transform. local Euler Angles. Then we are going to make our current Rotation Y value plus and equal to the “mouseX” local variable. Finally to make all these work we need the transform local Rotation.

What this script does is basically rotate the player according to the mouse position on the X axis, since the camera is a child object of the player, the rotation will occur as well. But to make the camera look up and down, we will need to access the main camera directly.

Similar to how we set up the rotation on the X axis, we need create another Vector3 called current Camera Rotation. This should be assign to the main camera game object. Then we are going to give the current camera rotation variable to follow the “mouseY” values. Finally just like with the horizontal rotation with the vertical rotation we need to make the main camera rotation value equal to the current camera rotation value.

To finish off, we need to call the mouse movement method within the Update movement otherwise nothing will happen.

Back in Unity, you will see now that the camera will work seamlessly as you move your mouse cursor.

--

--

Simon Truong

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.