The problem is when i switch between the cameras i see for a millisecond some speed value on the ui Text of the speed. On the Text cameraSpeed even if the cameras are not moving at all.
I need somehow either to track after the lastPosition or update lastPosition each time i switch to another camera.
This is the script i’m using to switch cameras:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwitchCameras : MonoBehaviour {
public Camera[] cameras;
public int currentCamera = 0;
void Awake()
{
if (cameras == null || cameras.Length == 0)
{
Debug.LogError("No cameras assigned..", gameObject);
this.enabled = false;
}
EnableOnlyFirstCamera();
}
void LateUpdate()
{
if (Input.GetKeyDown(KeyCode.C))
{
// disable current
cameras[currentCamera].enabled = false;
// increment index and wrap after finished array
currentCamera = (currentCamera + 1) % cameras.Length;
// enable next
cameras[currentCamera].enabled = true;
}
}
void EnableOnlyFirstCamera()
{
for (int i = 0; i < cameras.Length; i++)
{
// only 1==0 returns true
cameras[i].enabled = (i == 0);
}
}
}
And this is the script i’m using the update the ui objects i have in the Hierarchy like Texts.
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 > 0)
{
originalCamerasPosition = new Vector3[Cameras.Length];
for (int i = 0; i < Cameras.Length; i++)
{
originalCamerasPosition[i] = Cameras[i].transform.position;
}
}
}
// 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).magnitude / Time.deltaTime;
cameraSpeed.text = camSpeed.ToString();
lastPosition = Cameras[cameraIndex].transform.position;
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++;
}
}
}
- I’m updating lastPosition with the position of the current camera.
- I’m switching cameras.
- camSpeed is calculated using the difference between the camera’s current position and lastPosition.
Those are different positions, because lastPosition was the position of the last camera, not the current camera.
Maybe i should fix it by either tracking lastPosition per camera, or update lastPosition when you switch cameras ?
Not sure 100% if this is the problem and not sure how to fix it.
I have a UI empty GameObject and under it as children i have a Canvas > Panel and under Panel i have the buttons and texts and labels.
The Switch Cameras script is attached to another empty GameObject and under this one as children i have all the Cameras. Main Camera and two other Cameras.