---
title: "Moving the Camera"
content_type: tutorial
url: "https://learn.unity.com/course/roll-a-ball/tutorial/moving-the-camera-1?language=en"
language: en
container_type: course
container_title: "3D Beginner Game: Roll-a-Ball"
container_url: "https://learn.unity.com/course/roll-a-ball.md?language=en"
standalone_markdown_url: "https://learn.unity.com/tutorial/moving-the-camera-1.md?language=en"
previous_title: "Moving the Player"
previous_url: "https://learn.unity.com/course/roll-a-ball/tutorial/moving-the-player?language=en"
previous_markdown_url: "https://learn.unity.com/course/roll-a-ball/tutorial/moving-the-player.md?language=en"
next_title: "Setting up the Play Area"
next_url: "https://learn.unity.com/course/roll-a-ball/tutorial/setting-up-the-play-area-1?language=en"
next_markdown_url: "https://learn.unity.com/course/roll-a-ball/tutorial/setting-up-the-play-area-1.md?language=en"
difficulty: beginner
duration_minutes: 10
unity_version:
  - "6.0"
  - "6 Preview"
  - "6 Beta"
  - "2023.2"
  - "2023.1"
  - "2022.3"
  - "6.3"
supported_unity_versions:
  - name: "2020.2"
    url: "https://learn.unity.com/tutorial/moving-the-camera-1.md?language=en&version=2020.2"
  - name: "2020.1"
    url: "https://learn.unity.com/tutorial/moving-the-camera-1.md?language=en&version=2020.1"
  - name: "2019.4"
    url: "https://learn.unity.com/tutorial/moving-the-camera-1.md?language=en&version=2019.4"
  - name: "2019.3"
    url: "https://learn.unity.com/tutorial/moving-the-camera-1.md?language=en&version=2019.3"
  - name: "6.0"
    url: "https://learn.unity.com/course/roll-a-ball/tutorial/moving-the-camera-1.md?language=en&version=6.0"
  - name: "6 Preview"
    url: "https://learn.unity.com/tutorial/moving-the-camera-1.md?language=en&version=6+Preview"
  - name: "6 Beta"
    url: "https://learn.unity.com/tutorial/moving-the-camera-1.md?language=en&version=6+Beta"
  - name: "2023.2"
    url: "https://learn.unity.com/tutorial/moving-the-camera-1.md?language=en&version=2023.2"
  - name: "2023.1"
    url: "https://learn.unity.com/tutorial/moving-the-camera-1.md?language=en&version=2023.1"
  - name: "2022.3"
    url: "https://learn.unity.com/course/roll-a-ball/tutorial/moving-the-camera-1.md?language=en&version=2022.3"
  - name: "6.3"
    url: "https://learn.unity.com/course/roll-a-ball/tutorial/moving-the-camera-1.md?language=en&version=6.3"
last_updated: "2026-08-04T14:21:20.000Z"
---

# Moving the Camera

Position the Camera and script it to follow the Player sphere with a fixed offset in the Roll-a-ball project.

In this third Roll-a-ball tutorial, you’ll:

- Set the Camera position
- Write a script to make the Camera follow the Player sphere with a fixed offset

## Materials

- [Set_the_Camera_position-Transcript.txt](https://unity-connect-prd.storage.googleapis.com/20200716/798f152b-b423-4b19-bd35-a5c598819b8c/Set_the_Camera_position-Transcript.txt)
- [Write_a_CameraController_script-Transcript.txt](https://unity-connect-prd.storage.googleapis.com/20200716/3cd8af1b-f4bb-416e-8347-9212c22812e6/Write_a_CameraController_script-Transcript.txt)
- [Reference_the_Player_GameObject-Transcript.txt](https://unity-connect-prd.storage.googleapis.com/20200716/ef137e2f-3ffd-40f8-9254-a9944eb95d4d/Reference_the_Player_GameObject-Transcript.txt)

## 1. Overview

In this tutorial, you’ll:

- Set the Camera position
- Write a script to make the Camera follow the Player sphere with a fixed offset

By the end of this tutorial, your game will look something like this:

[Video](https://connect-mediagw.unity.com/h1/20230920/videos/7651cc5e-a609-48d8-8512-47a79c6ab081_3.3.2-final-move-the-camera.mp4)

## 2. Set the Camera position

Follow the video or text instructions below to raise and rotate the camera, then experiment with the **Player** GameObject as a parent GameObject:

[Video](https://connect-mediagw.unity.com/h1/20211215/videos/9db72c77-e1fc-4337-834a-014ffb6f043c_Unity_RollABall_3_1.mp4)

[Download Transcript ](https://connect-prd-cdn.unity.com/20200716/798f152b-b423-4b19-bd35-a5c598819b8c/Set_the_Camera_position-Transcript.txt?_ga=2.73947674.943030393.1594530423-42703541.1592249374)

## 1\. Raise and rotate the camera.

- Select the **Main Camera** in the **Hierarchy** window.
- Set the **Y** **position** value to around **10**.
- Set the **X rotation** value to around **45**.
- Preview the camera angle in the **Game** view.


## 2\.  Experiment with the Player as a parent GameObject.

- If you want, drag the **Main Camera** onto the **Player** GameObject in the **Hierarchy** window to set the camera as a child GameObject of the **Player** GameObject.
- Test the game and notice how the camera rotates with the movement of the **Player** GameObject.
- Undo the parent-child relationship by dragging the **Main Camera** above the **Player** GameObject.

![](https://connect-mediagw.unity.com/h1/20230920/learn/images/da082e94-8068-4319-8640-3ebdf9d2c873_1-game-view-camera.png)

## 3. Write a CameraController script

Follow the video or text instructions below to create the **CameraController** script, declare player and offset variables, define the offset variable, then set the camera position in **LateUpdate**:

[Video](https://connect-mediagw.unity.com/h1/20211215/videos/242f386a-7927-4846-9eba-d7c9c0cf511f_Unity_RollABall_3_2.mp4)

[Download Transcript](https://connect-prd-cdn.unity.com/20200716/3cd8af1b-f4bb-416e-8347-9212c22812e6/Write_a_CameraController_script-Transcript.txt?_ga=2.11648572.943030393.1594530423-42703541.1592249374)

## 1\.  Create the CameraController script.

- With the **Main Camera** GameObject selected in the **Hierarchy** window, select **Add Component** > **New script** in the **Inspector** window.
- Name your new script “CameraController”.
- In the **Project** window, move the script from the root **Assets** folder into the **Scripts** folder.
- Open the new script for editing.


## 2\.  Declare player and offset variables.

- Inside the first curly brace, add the following lines of code to declare two new variables:

```csharp
public GameObject player;
private Vector3 offset;
```

## 3\.  Define the offset variable.

- In the **Start** function, on a new line after the first curly brace, add the following line of code:

```csharp
offset = transform.position - player.transform.position; 
```

## 4\.  Set the camera position in LateUpdate.

- In the **Update** function, inside the first curly brace, add the following line of code:

```csharp
transform.position = player.transform.position + offset; 
```

- Change the Update function to a LateUpdate function:

```csharp
void LateUpdate()
```

![](https://connect-mediagw.unity.com/h1/20230920/learn/images/c7e649a1-9161-4f00-bd0a-8db14563678a_2-new-camera-controller-script.png)

## 4. Reference the Player GameObject

Follow the video or text instructions below to assign the **Player** GameObject variable in the **Inspector** window, then test the game:

[Video](https://connect-mediagw.unity.com/h1/20211215/videos/1f31a846-962d-4830-8c95-bd248acf0ee3_Unity_RollABall_3_3.mp4)

[Download Transcript ](https://connect-prd-cdn.unity.com/20200716/ef137e2f-3ffd-40f8-9254-a9944eb95d4d/Reference_the_Player_GameObject-Transcript.txt?_ga=2.118984961.943030393.1594530423-42703541.1592249374)

## 1\.  Assign the Player GameObject variable in the Inspector window.

- Make sure you have saved the **CameraController** script, then return to Unity.
- In the **Hierarchy** select the **MainCamera** GameObject, then drag the **Player** GameObject from the **Hierarchy** window into the **Player** slot in the **CameraController** component.


![](https://connect-mediagw.unity.com/h1/20230920/learn/images/53f048b9-368c-455e-a2df-739147d09906_3-assign-player-variable.png)

## 2\.  Test the game.

- The camera should now follow the player at the appropriate offset.

[Video](https://connect-mediagw.unity.com/h1/20230920/videos/4d66dec6-d789-4ffb-9451-c4eb8c7a34cb_3.3.2-final-move-the-camera.mp4)

## 5. Final script sample _(Optional)_

If your script is not working as expected, below is an example of what your code should look like at this point. The comments have been added to make the code more readable.

## CameraController.cs

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour
{
 // Reference to the player GameObject.
 public GameObject player;

 // The distance between the camera and the player.
 private Vector3 offset;

 // Start is called before the first frame update.
 void Start()
    {
 // Calculate the initial offset between the camera's position and the player's position.
        offset = transform.position - player.transform.position; 
    }

 // LateUpdate is called once per frame after all Update functions have been completed.
 void LateUpdate()
    {
 // Maintain the same offset between the camera and player throughout the game.
        transform.position = player.transform.position + offset;  
    }
}
```
