The first script is the way I'm using now to switch between cameras.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SwitchCameras : MonoBehaviour
{
[Header("Cameras Init")]
public Camera[] cameras;
public Vector3[] originalPosition;
[HideInInspector]
public Vector3 currentCameraOriginalPosition, currentCameraPosition;
[Space(5)]
[Header("Cameras Switch")]
public string currentCameraName;
public Vector3[] lastCameraPosition;
private int currentCamera = 0;
void Start()
{
cameras = Camera.allCameras;
lastCameraPosition = new Vector3[cameras.Length];
if (cameras.Length >= 1)
{
originalPosition = new Vector3[cameras.Length];
for (int i = 0; i < cameras.Length; i++)
{
originalPosition[i] = cameras[i].transform.position;
}
}
if (cameras.Length == 1)
{
Debug.LogError("Need more then 1 camera for switching..");
}
else
{
Debug.Log("Found " + cameras.Length + " cameras");
}
for (int i = 0; i < cameras.Length; i++)
{
cameras[i].enabled = false;
}
cameras[0].enabled = true;
currentCameraName = cameras[0].name;
currentCameraOriginalPosition = originalPosition[0];
}
void LateUpdate()
{
if (Input.GetKeyDown(KeyCode.G))
{
cameras[currentCamera].enabled = false;
if (++currentCamera == cameras.Length)
currentCamera = 0;
cameras[currentCamera].enabled = true;
currentCameraName = cameras[currentCamera].name;
currentCameraOriginalPosition = originalPosition[currentCamera];
}
lastCameraPosition[currentCamera] = cameras[currentCamera].transform.position;
currentCameraPosition = lastCameraPosition[currentCamera];
}
}
And this is a script for starting a new game:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewGame : MonoBehaviour
{
public void RestartGame()
{
}
}
Both scripts attached to the same GameObject. I'm calling the RestartGame method from On Click in the inspector of a button I have in the main menu.
Now in the SwitchCameras script I'm using the G key to switch between the cameras.
But now I want to do that when a new game start to fade in/out between two cameras inside the RestartGame method. I want to call some method from the SwitchCameras script that will gade in/out between two cameras.
The idea is when starting a new game or resuming a game that the first camera will fade out slowly, fade out I mean will be enabled false slowly and then near the end to enabled true the second camera slowly.
Like switching between the two cameras but slowly.
Should i use startcoroutine ?
true
andfalse
. If you want them to fade then both need to be active at the same time and one of them needs to render....transparently...which they can't do. You need some kind of transition graphic. \$\endgroup\$