using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SwitchCameras : MonoBehaviour
{
// On start init
public Camera[] cameras;
public Vector3[] originalPosition;
// On switch
public Camera currentCameraName;
public Vector3 lastCameraPosition;
public float cameraSpeed;
// On target when target exist
public float nextTargetDistance;
public Vector3 nextTargetPosition;
public Transform nextTarget;
private int currentCamera = 0;
void Awake()
{
cameras = GameObject.FindObjectsOfType<Camera>();
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");
}
}
In the Awake i want to enable true the first camera in the array and and to enable false the rest of the cameras. So when running the game each time only the first camera in the array will be enabled true and the rest enabled false.
For the second problem. The script is for switching between cameras.
The problem is when i’m running the game the active camera is the Main Camera but in the array the first camera is not the Main Camera. So when i’m using the array in another script i get mixed information. I show the first camera in the array but the active camera is the Main. Until i click on C and switch for the Main Camera.
So i need to enabled true the first camera that’s [0] but also to enabled false all the others.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SwitchCameras : MonoBehaviour
{
// On start init
public Camera[] cameras;
public Vector3[] originalPosition;
// On switch
public Camera currentCameraName;
public Vector3 lastCameraPosition;
public float cameraSpeed;
// On target when target exist
public float nextTargetDistance;
public Vector3 nextTargetPosition;
public Transform nextTarget;
private int currentCamera = 0;
void Awake()
{
cameras = GameObject.FindObjectsOfType<Camera>();
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");
}
}
void LateUpdate()
{
if (Input.GetKeyDown(KeyCode.C))
{
cameras[currentCamera].enabled = false;
if (++currentCamera == cameras.Length)
currentCamera = 0;
cameras[currentCamera % cameras.Length].enabled = true;
lastCameraPosition = cameras[currentCamera].transform.position;
cameraSpeed = (cameras[currentCamera].transform.position - lastCameraPosition).magnitude / Time.deltaTime;
Debug.Log(cameras[currentCamera].name + " Last position " + cameras[currentCamera].transform.position);
}
}
}
Then in another script i’m displaying using gui some information about the cameras:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UserInformation : MonoBehaviour
{
public Text cameraName;
public Text cameraSpeed;
public Text cameraOriginalPosition;
public Text nextTarget;
public Text nextTargetPos;
public Text nextTargetDis;
public SwitchCameras switchcameras;
public Transform target;
private Vector3[] originalCamerasPosition;
private Vector3[] lastPosition;
private Camera[] Cameras;
private int cameraIndex = 0;
// Use this for initialization
void Start()
{
Cameras = switchcameras.cameras;
if (Cameras.Length >= 1)
{
originalCamerasPosition = new Vector3[Cameras.Length];
for (int i = 0; i < Cameras.Length; i++)
{
originalCamerasPosition[i] = Cameras[i].transform.position;
}
lastPosition = new Vector3[Cameras.Length];
//lastPosition = originalCamerasPosition;
}
}
// Update is called once per frame
void Update()
{
if (cameraIndex == Cameras.Length)
cameraIndex = 0;
if (Cameras[cameraIndex].enabled == true)
{
cameraName.text = Cameras[cameraIndex].name;
float camSpeed = (Cameras[cameraIndex].transform.position - lastPosition[cameraIndex]).magnitude / Time.deltaTime;
lastPosition[cameraIndex] = switchcameras.lastCameraPosition;
cameraSpeed.text = camSpeed.ToString();
cameraOriginalPosition.text = originalCamerasPosition[cameraIndex].ToString();
if (target != null)
{
nextTarget.text = target.ToString();
nextTargetPos.text = target.transform.position.ToString();
var distance = Vector3.Distance(Cameras[cameraIndex].transform.position, target.transform.position);
nextTargetDis.text = distance.ToString();
}
}
else
{
cameraIndex++;
}
}
}
The problem is for example in the:
cameraSpeed.text = camSpeed.ToString();
Since the Main Camera is the only camera that move at the moment the speed value it’s showing is for the Main Camera but the others are for the first camera in the array.
In the screenshot example the camera name is follow camera but it’s showing the movement speed of the main camera:
Follow Camera is not moving at all.
In the Camera Speed it;s the Main Camera movement speed not of the Follow Camera.
Only when i will click on C until i will be back again to the Follow Camera it will be fine.
The problem is when i’m running first time the game. The active camera is Main Camera but it’s showing the first camera in the array Follow Camera.
So either when running first time the game show the current active camera first and enabled false the others or enabled false the other and show the first camera in the array.