0
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SwitchCameras : MonoBehaviour
{
    public List<Camera> cameras = new List<Camera>();

    private Vector3 originalPos;

    // Start is called before the first frame update
    void Start()
    {
        originalPos = transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void LateUpdate()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (cameras[0].enabled)
            {
                transform.position = new Vector3(transform.position.x,
                    transform.position.y - 0.7f, transform.position.z);

                cameras[1].enabled = false;
            }
            else
            {
                cameras[1].enabled = false;
            }
        }
    }
}

Because transform have animator component the positions and switching is working in the late update.

My problem is the cameras switching and the positions change of the transform logic in the late update when pressing the space key.

I want that each press on the space key will switch between the cameras. First in the awake or start I need somehow to find what camera is first enabled true and make the changes than in the late update with the space key.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$
private void LateUpdate()
{
    if (Input.GetKeyDown(KeyCode.Space))
    {
        if (cameras[0].enabled)
        {
            transform.position = new Vector3(transform.position.x,
                transform.position.y - 0.7f, transform.position.z);

            cameras[0].enabled = false;
            cameras[1].enabled = true; //<-----
        }
        else
        {
            cameras[0].enabled = true;  //<-----
            cameras[1].enabled = false;  //<-----
        }
    }
}

I have indicated the places where I made changes with //<-----. Usually I try to write an accompanying explanation but this is so straightforward I can't think of any explanation to add.

\$\endgroup\$

You must log in to answer this question.

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