Imanoodle
Imanoodle

Reputation: 11

How to change displays when switching between cameras

I'm trying to make a code that switches cameras based in the value of an Input Axis by activating the different cameras.

This is what the code looks like:

public class CameraManager : MonoBehaviour
{

    public Camera mainCamera;
    public Camera rightCamera;
    public Camera leftCamera;

    public float axisValue;

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

    // Update is called once per frame
    void Update()
    {
        // gets value of "Camera Position" value
        axisValue = Input.GetAxis("Camera Position");
        ToggleCamera();
    }

    // manages cameras based on axis values
    public void ToggleCamera()
    {
        if (axisValue > 0)
        {
            SetRightCamera();
        }
        else
        if (axisValue < 0)
        {
            SetLeftCamera();
        }
        else 
        if (axisValue == 0)
        {
            SetMainCamera();
        }
    }

    // Sets camera to the mainCamera
    void SetMainCamera()
    {
        mainCamera.enabled = true;
        rightCamera.enabled = false;
        leftCamera.enabled = false;
    }

    // Sets camera to rightCamera
    void SetRightCamera()
    {
        mainCamera.enabled = false;
        rightCamera.enabled = true;
        leftCamera.enabled = false;
    }

    // Sets camera to leftCamera
    void SetLeftCamera()
    {
        mainCamera.enabled = false;
        rightCamera.enabled = false;
        leftCamera.enabled = true;
    }
}

This code does in fact enable and disable each camera but it does not display the enabled cameras (I would have to manually change the Display in the game view).

Is there a way to script so that the game would change the display based on the input axis?

Upvotes: 0

Views: 78

Answers (2)

Imanoodle
Imanoodle

Reputation: 11

I'm realizing now that I should have specified my purpose with the cameras. Displays are used for adding a second view of your game on the same monitor at the same time, as shown in the documentation provided by Thomas.

What I wanted was a change of view of the player by pressing a button (think of playing a dogfighting game and pressing a button to see what's behind you.) The solution to this is actually quite simple, more or less:

1.) (Assuming the Main Camera already exists) Create a new camera or cameras in the Hierarchy [right-click on Hierarchy > Camera]

2.) Set the camera(s)' Target Display to Display 1 (changing view will not work if the cameras' Target Displays are different.)

3.) Make a new GameObject called 'CameraManager' and make a new script as well.

4.) Rather than using Display.Activate(); or gameObject.enable = true;, instead use gameObject.SetActivate(true);, this basically disables and enables the cameras depending on what button you are using to switch between the cameras.

This is what the final code looks like:

public GameObject mainCamera;
public GameObject rightCamera;
public GameObject leftCamera;
public GameObject firstPerson;

public bool povToggle;

private float axisValue;

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

// Update is called once per frame
void Update()
{
    // gets value of "Camera Position" value
    axisValue = Input.GetAxis("Camera Position");
    ToggleCamera();
}

// manages cameras based on axis values
public void ToggleCamera()
{
    if (axisValue > 0)
    {
        SetRightCamera();
    }
    else
    if (axisValue < 0)
    {
        SetLeftCamera();
    }
    else 
    if (axisValue == 0)
    {
        SetMainCamera();
    }
}

// Old code
/*
void SetMainCamera()
{
    mainCamera.enabled = true;
    rightCamera.enabled = false;
    leftCamera.enabled = false;
}

// Sets camera to rightCamera
void SetRightCamera()
{
    mainCamera.enabled = false;
    rightCamera.enabled = true;
    leftCamera.enabled = false;
}

// Sets camera to leftCamera
void SetLeftCamera()
{
    mainCamera.enabled = false;
    rightCamera.enabled = false;
    leftCamera.enabled = true;
}
*/

// Sets camera to the mainCamera
void SetMainCamera()
{
    mainCamera.SetActive(true);
    rightCamera.SetActive(false);
    leftCamera.SetActive(false);
}

// Sets camera to rightCamera
void SetRightCamera()
{
    mainCamera.SetActive(false);
    rightCamera.SetActive(true);
    leftCamera.SetActive(false);
}

// Sets camera to leftCamera
void SetLeftCamera()
{
    mainCamera.SetActive(false);
    rightCamera.SetActive(false);
    leftCamera.SetActive(true);
}

Upvotes: 1

Thomas
Thomas

Reputation: 1257

You cannot change the display you are using freely. First you have to configure a multi screen setup (telling Unity which displays to use) by activating said displays (see Display.Activate() in documentation). Afterwards Unity will always use/occupy those diplays, even if you do not render onto them.

To render to a display you have to set the target display of a camera to that display (e.g. via the inspector) (again documentation). Afterwards the cameras will target the correct display.

However you can not test this functionality in the editor, but have to build your application (if I recall correctly from my last project using this feature).

Upvotes: 0

Related Questions