- Home /
How can i get in real time the speed movement of each camera ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UserInformation : MonoBehaviour
{
public Text cameraText;
public Text cameraSpeed;
public Text cameraOriginalPosition;
public Transform target;
public SwitchCameras switchcameras;
private Vector3[] prevCamerasPosition;
private Camera[] Cameras;
private int cameraIndex = 0;
// Use this for initialization
void Start()
{
Cameras = switchcameras.cameras;
if (Cameras.Length > 0)
{
prevCamerasPosition = new Vector3[Cameras.Length];
for (int i = 0; i < Cameras.Length; i++)
{
prevCamerasPosition[i] = Cameras[i].transform.position;
}
}
}
// Update is called once per frame
void Update()
{
if (cameraIndex == Cameras.Length)
cameraIndex = 0;
if (Cameras[cameraIndex].enabled == true)
{
cameraText.text = Cameras[cameraIndex].name;
//cameraSpeed.text =
cameraOriginalPosition.text = prevCamerasPosition[cameraIndex].ToString();
}
else
{
cameraIndex++;
}
}
}
I can get the name and original start position but now i need to get the speed in real time and assign it to: cameraSpeed.text = I want to calculate and display the real time of each camera movement speed.
Another sub question: Do i need to change the Update to FixedUpdate or LateUpdate ?
Answer by webcam · Jun 19, 2017 at 03:25 PM
Hey mate, you need to store the position of the camera in the last update and then check the difference in position this update.
Vector3 lastPosition
void Update()
{
float camSpeed = (transform.position - lastPosition).magnitude / Iime.deltaTime;
cameraSpeed.text = camSpeed.ToString();
lastPosition = transform.position;
}
In the future check around lots of people have asked similar questions here.
Your answer
Follow this Question
Related Questions
Why when creating new animator controller for the character the character is not walking right ? 0 Answers
How can i Instantiate on the terrain from left to right ? 0 Answers
Why the tile map scripts take almost all the cpu usage ? cpu usage is getting to 99% at times 1 Answer
How can i rotate object by pressing on key R and keep object facing to me my self ? 0 Answers
How can i spawn new gameobjects to be inside the terrain area ? 2 Answers