Player Controls (New Input System)

Cycling between Cameras in order using Player Input

You don’t have access to this lesson
Please register or sign in to access the course content.

Sometimes in a scene you want to be able to switch between different cameras to get a different viewpoint.

In this lesson you will learn how to:

  • Use code to set different Camera objects to being active or inactive
  • Use a list to store multiple camera objects

You will need to know:

  • Setup the New Input System
  • Create a scene with multiple cameras

Setup a scene with multiple cameras or download the one linked to this lesson.

Add the New Input System to the project.

Create a GameController / GameManager object using Empty GameObject

On the GameController add a Player Input component and setup default player inputs (or your own custom inputs).

Create a new script. In this example it is CycleCamera

Open the script in your editor of choice.

Add the Input system to the script.

Now we need to set up some variables.

First set up a list of Camera objects to store all of the cameras in the scene.

This will be a public variable.

Then set up a public variable for the currentCamera index

This is public so that we can customise if from within Unity and it is set to 0 which will be our first camera.

We are not going to write a function to turn off all the cameras and then turn on a camera that we want. This camera will be identified by it’s index in the List.

The code below creates a function called SwitchToCamera that takes one parameter the index of the Camera to switch to.

Now we need to include the code to turn off all of the cameras.

We do this by iterating over the cameras.

The for loop will create an int that starts at 0 and then runs until it reaches the number (Count) of the cameras. Once the loop runs i will be incremented by 1.

Inside the for loop we access the current camera in the list by using cameras[i] to get the index of the camera in the list.

We then use dot notations to get the gameObject and then access the SetActive method to false.

This will run through all cameras and disable them.

Now we need to set the camera we want to be active.

Outside of the for loop we use the camIndex parameter to access the camera we want to activate and change the SetActive variable to true to enable it.

Now we want to run this when the Scene loads.

In the Start method add a call to run SwitchToCamera(currentCamera);

This will switch the initial camera to the one we set in Unity.

Now we are going to test our code.

Switch back to Unity.

Add the Cycle Camera script to the GameManager as a component.

We now need to configure it.

First we need to add the cameras to the list.

Next to cameras change the 0 to the number of cameras that you have and then open the List by clicking on the triangle.

Click on the target and add each Camera to the list.

Note the order here will be the order they cycle through when we implement this.

Set the currentCamera to use initially.

Here we have switched to camera[2) which is the Camera3 object in the scene.

Run the program to test it.

Here we can see the Scene loads with camera 2.

Now switch back to the CycleCamera script.

Add an OnFire function (or other player input action event that you with to run for switching the camera)

We now need to cycle through the cameras.

Add an if statement to check if the next camera is beyond the end of the list of cameras. e.g. Trying to get to camera 4 if there are three cameras.

We have to add +1 to check the next camera.

If this valid we add 1 to the currentCamera value.

Next we need to add an else clause to reset the camera back to 0 or the first camera in the list..

After this check we run the SwitchToCamera function we wrote earlier and pass it the currentCamera value

Test your program.

Click the fire button and you should cycle through the different cameras.

Below is the Completed CycleCamera code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class CycleCamera : MonoBehaviour
{
    public List<Camera> cameras;

    public int currentCamera = 0;

    void SwitchToCamera(int camIndex)
    {
        for(int i = 0; i < cameras.Count; i++)
        {
            cameras[i].gameObject.SetActive(false);
        }
        cameras[camIndex].gameObject.SetActive(true);
    }

    // Start is called before the first frame update
    void Start()
    {
        SwitchToCamera(currentCamera);   
    }

    void OnFire()
    {
        if(currentCamera + 1 < cameras.Count)
        {
            currentCamera = currentCamera + 1;
        } else
        {
            currentCamera = 0;
        }
        SwitchToCamera(currentCamera);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}