-1
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SwitchCameras : MonoBehaviour
{
    [Header("Cameras Init")]
    public Camera[] cameras;
    public Vector3[] originalPosition;

    [Space(5)]

    [Header("Cameras Switch")]
    public string currentCameraName;
    public Vector3[] lastCameraPosition;
    public float cameraSpeed;

    [Space(5)]

    [Header("Cameras Target")]
    public float nextTargetDistance;
    public Vector3 nextTargetPosition;
    public Transform nextTarget;

    private int currentCamera = 0;

    void Start()
    {
        cameras = Camera.allCameras;
        lastCameraPosition = new Vector3[cameras.Length];

        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");
        }

        for (int i = 0; i < cameras.Length; i++)
        {
            cameras[i].enabled = false;
        }
        cameras[0].enabled = true;
        currentCameraName = cameras[0].name;
    }

    void LateUpdate()
    {
        if (Input.GetKeyDown(KeyCode.C))
        {
            cameras[currentCamera].enabled = false;

            if (++currentCamera == cameras.Length)
                currentCamera = 0;

            cameras[currentCamera % cameras.Length].enabled = true;
            cameraSpeed = (cameras[currentCamera].transform.position - lastCameraPosition[currentCamera]).magnitude / Time.deltaTime;
            lastCameraPosition[currentCamera] = cameras[currentCamera].transform.position;
            currentCameraName = cameras[currentCamera].name;

            Debug.Log(cameras[currentCamera].name + " Last position " + cameras[currentCamera].transform.position);
        }
    }
}

Two things i want to do:

  1. When running the game display the movement speed of cameras[0] in the Update i want to see the speed in real time not only once like still.

  2. Then when i click on C to switch between the cameras display each camera in real time the movement speed. Now when i click on C and switch between the cameras i see still speed value and not real time running value of the speed.

\$\endgroup\$
4
  • \$\begingroup\$ So you want to display the speed value each frame? Is that your problem? You're only getting one line in Debug.Log until you change the camera? The question is a little unclear but that might just be me not understanding the problem. \$\endgroup\$
    – user35344
    Commented Aug 25, 2017 at 13:35
  • 3
    \$\begingroup\$ I'm voting to close this as unclear. I cannot parse the sentences you have written in those two list items. Also, "I want to do thing." is not a question -- what trouble are you having with doing that thing? What have you done and where are you stuck? What do you need help with in doing it specifically? Further, since this is two different issues, you should be asking this in separate questions. (Probably one after the other.) \$\endgroup\$ Commented Aug 25, 2017 at 13:40
  • \$\begingroup\$ When you hit c to switch camera's the value you see in the inspector will be for the selected game object not the currently active camera. If you want to see the camera speed in the logger, you should probably add it to your Debug.Log statement. Is this script on a camera or an empty gameobject? \$\endgroup\$
    – AresCaelum
    Commented Aug 25, 2017 at 13:49
  • \$\begingroup\$ @Tyyppi_77 You right each frame. And not to see it in the logger but in the Inspector. What i want to do is to see the speed movement of the current active camera when running the game and then when switching between the cameras. See the speed i mean to show it in the inspector each frame. \$\endgroup\$ Commented Aug 25, 2017 at 14:15

1 Answer 1

1
\$\begingroup\$

So I think you want to move the speed calculation out of your key-check if-statement. So your late update function should look something like this:

void LateUpdate()
{
    if (Input.GetKeyDown(KeyCode.C))
    {
        cameras[currentCamera].enabled = false;

        if (++currentCamera == cameras.Length)
            currentCamera = 0;

        cameras[currentCamera].enabled = true;
        currentCameraName = cameras[currentCamera].name;
    }

    cameraSpeed = (cameras[currentCamera].transform.position - lastCameraPosition[currentCamera]).magnitude / Time.deltaTime;
    lastCameraPosition[currentCamera] = cameras[currentCamera].transform.position;

    Debug.Log(cameras[currentCamera].name + " Last position " + cameras[currentCamera].transform.position);
}

So here, when the key is pressed, we only update the camera index and name, and leave the speed calculation out of there. The actual speed calculation is now done each frame instead of only when a key is pressed.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .