I want the camera to switch 1-4
Controls
Left Arrow - Previous Camera
Right Arrow - Next Camera
I’ve come to asking this as my last resort because i have checked all over google…
I want the camera to switch 1-4
Controls
Left Arrow - Previous Camera
Right Arrow - Next Camera
I’ve come to asking this as my last resort because i have checked all over google…
There are many different ways to do this, but the easiest and most straightforward way is to:
Here’s a fully-commented example:
public Camera[] cameras; // Contains each of the cameras
int index = 0; // Corresponds to the cameras array
void Update ()
{
// Left-Arrow Key
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
// Decrease the index
index--;
// If the index goes below 0, wrap back around to the max number (defined by the array's size)
if (index < 0)
index = cameras.Length - 1;
// Set the camera to whichever one is located at cameras[index]
ChangeCamera(index);
}
// Right-Arrow Key
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
// Increase the index
index++;
// If the index goes beyond the max number, wrap back around to 0
if (index > cameras.Length - 1)
index = 0;
// Set the camera to whichever one is located at cameras[index]
ChangeCamera(index);
}
}
// Performs the actual camera-swapping
void ChangeCamera (int camIndex)
{
// Loop through the entire cameras array, enabling the correct camera and disabling the others
for (int i = 0; i < cameras.Length; i++)
{
// If i matches the current index, enable the camera at that position in the array
if (i == index)
cameras*.enabled = true;*
// Otherwise, disable the camera at that position in the array
else
cameras*.enabled = false;*
}
}