Character Controller 101: 3D Development — Camera Controller

Christopher Pearl
5 min readAug 17, 2022

--

When building a game, Unity comes with a main camera ready for you in the Hierarchy from the start. But what if you wanted to have a different POV for a first player shooter game or add custom controls to give the user more freedom to view your game/film world? Let’s build a custom camera controller so your character can see more angles of your one of a kind game creation.

Following the Player

To get started, lets drag and drop the Main Camera onto the Player ( or any object you picked in the Hierarchy). This we make the camera a child to player game object. By pressing play, you’ll see that the camera will now follow behind the game object. For now, you can readjust the angles to your liking.

Looking With the Camera

Now, it’s time to create our logic for the Camera Controller. The logic will live in the Player class. Because this will be used on a laptop/desktop, we will be using a mouse to control the player’s ability to look around the 3D landscape. In Project Settings, by going to Input Manager, you can see what name is labeled for both Mouse inputs (Mouse X, Mouse Y). We will use this as a reference for creating the logic for how our camera moves.

We’ll first move our calculations of movement into its own method, called CalculateMovement(). Then we will put the input variables needed in Update(). We’ll be using two float variable to store the input and use the mouse reference names from the Input Manager above.

Next, we need to locate the axis that we want to control for the rotation of our camera to look up and down and left to right. For looking left to right, we want to grab the rotation of the Y axis on the Player object. For looking up and down, we’ll want to get the X axis on the Camera, and not the Player.

Rotation of Y on Player for looking left and right
Rotation of X on Camera for looking up and down

To look left and right, we’ll create a Vector3 variable and store the euler angles of the Player. We’ll then append the rotation on y axis of the rotation to the mouseX movement. Finally, we’ll store the new rotation in the localRotation of the player.

Euler angles can represent a 3 dimensional rotation in world space. Here, we’ll be getting the current rotation, calculating it with the mouseX movement, restoring the data in current rotation.

Quaternions prevents gimbal lock and are used to represent rotation. They are much better to use for optimization of your code (interpolation — smoothing out the physics used when calculating your logic).

Gimbal Lock

To look up and down, we’ll need to first create a camera variable. Then, in Start(), let’s set the Camera object to so to make sure the camera isn’t null.

Now, let’s get the euler angles of the main camera, append this to the mouseY input, and then store the Quaternion rotation in the localRotation just like the mouseX above. For the up and down movement, you will this to be inverted. We’ll need to subtract on mouseY, not add.

Learning lesson: After Debugging due to a logic error, I found some information on storing your Quaternion rotation in rotation vs. localRotation..make sure you’re moving on the correct axis.

Local Space to World Space

To prevent any change rotation to, it’s best to use localrotation, rather than rotation to store your Quaternion rotations. Why? Because your camera is child of the player and the player is already in world space, it will prevent unwanted movement of the camera when Unity is performing the logic calculations.

Looking Forward

Finally, to make your player look in the proper direction, especially when moving left and right, you’ll want to make sure that the direction the player is moving is the forward position of the camera.

Let’s set your player’s velocity in the forward direction of the camera. Set velocity to transform.TransformDirection. You’ll be putting this line of code in the CalculateMovement() method.

Place this line of Code in CalculateMovement()

Voila! Go back into Unity, press Play, and move your mouse. You now have a fully functioning Camera Controller!

Player Controller looking all over the place!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Christopher Pearl
Christopher Pearl

Written by Christopher Pearl

Unity/Unreal Developer, Artificial Intelligence Specialist, AR/VR Developer

No responses yet

Write a response