Third person camera setup in Unity

Daniel Kirwan
3 min readOct 4, 2021

--

Objective: Start making a third person camera in Unity

The starting point for this camera is for use with the mouse and keyboard. First thing to do is to add the camera as a child object to the player.

You will then need to move the camera to your desired location. For me I’m making a third person camera so I want the camera a little behind the player and looking slightly down.

Now, I need to set up the camera movement. I am doing this in my Player class, you can do this in a separate camera class if you desire.

I created a method for the CameraMovement and call this in Update. The next step is to get the input from the mouse. You can find the inputs in inside Unity -> project settings -> input manager. You need to look for the mouse X and mouse Y axes.

The highlighted names are the strings that we need to use in the code to get the position of the mouse in the x and y positions.

Now we can set the x and y mouse positions to two new variables which will be used for the camera movement.

I will focus on moving the camera in the mouse x position.

The above will move the player locally and not in world space. This will turn the player and the camera.

Now we need to add the Y movement.

For this to work correctly the last line needs to be localRotation, if it is just rotation, you will get movement like the below video. It just looks weird.

This should now all be working and you will have a working camera for a third person game. But, you might find that your camera and player turn but your player still moves in the wrong direction and not to where the camera is pointing. This is because the player is moving is world space and not local space.

I am using a character controller for my player, so I’m using the move method to make the player move. But before I call this method I need to change my player velocity by using using transform.TransformDirection.

This line will now make the player move in the direction the camera is facing when I’m pressing the forward button.

--

--

Daniel Kirwan
Daniel Kirwan

No responses yet