- Home /
How can i track/update the lastPosition of a camera when switching between cameras ?
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.
Seems like you are over complicating this a bit, how many camera are going to be using in total because if its only 2 or 3 then I would $$anonymous$$.i.s.s this solution a bit.
I would simply add the cameras and name them 1 2 3 and then just switch to them and store the vector3 position and quaternions as variables. By keeping it that simple there is very little that can go wrong.
The switching cameras script is now for 3 cameras but i want it to be for any number of cameras not only 2-3.
Could you show me example of how to do it i the switch cameras script with storing the positions and quaternions ?
And then how do i pass it/using it with the UserInformation script ?
Your answer
Follow this Question
Related Questions
How can I switch between two cameras ? 1 Answer
How do I tilt my camera when I'm doing a certain action 1 Answer
Problem with a Unity Scripting example from the scripting manual! 0 Answers
How can i make the circle to be drawn not from the top but from the camera view top ? 1 Answer
How can I instantiate a prefab/s in all loaded scenes or in selected scenes from list ? 1 Answer