Camera switch between child back to main camera issue

Hi All,

It is my first post here.
Need a bit of help.

  • Scenario:

Player is walking around and by interacting with the object, the camera is then switch to focus on that object. (It has to switch to the 2nd camera)

I do have a Raycast in place and it does refer to the 2nd script - I am able to switch to the second camera.

The problem is, I am unable to switch back :frowning:

Here is the second script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MySelectedPainting : MonoBehaviour
{
    public Camera playerCamera;
    public Camera artCamera; //No longer needed, replaced with GameObject child1
   
    [SerializeField] private KeyCode goBackToPlayerKey = KeyCode.Q;
    
    public void SwitchCamera()
    {
        GameObject originalGameObject = GameObject.Find("PaintingLarge");
        GameObject child1 = originalGameObject.transform.GetChild(0).gameObject;
        Debug.Log("Painting" + child1.name);
        playerCamera.enabled = false;
        artCamera.enabled = true;

        if (Input.GetKeyDown(goBackToPlayerKey))
        {
            Debug.Log("Switch?");
            playerCamera.enabled = true;
            artCamera.enabled = false;
        }
    }

Any help will be much appreciated.

I don’t know how SwitchCamera is called but I guess it’s a one time event, right? So SwitchCamera isn’t called every frame. That means that the Input checking code won’t run, meaning that the switch back to player camera won’t run. I suggest that you place the input code in Update, so it gets updated each frame.

void Update()
{
         if (Input.GetKeyDown(goBackToPlayerKey))
         {
             Debug.Log("Switch?");
             playerCamera.enabled = true;
             artCamera.enabled = false;
         }
}

Hi @DariuszUK , I did’nt understand why your exactly using find method but it’s an expensive call, specially on the update call, so I recommend you don’t use it this way but since I didn’t know why it’s been called I didn’t changed it, anyway this is how to do it, you should add a bool field called isCameraFocused and change it accordingly to your key like a toggle, so every time you hit the key you go to other camera and set off the other one.
Also keep in mind that camera.enable = true or false will enable or disable code COMPONENT of camera not the object (of course it will work probably but I recommend you to set off or on the whole object), anyway let me know if you had any problem. good luck, cheers.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MySelectedPainting : MonoBehaviour
 {
     public Camera playerCamera;
     public Camera artCamera; //No longer needed, replaced with 
GameObject child1
//To change between cameras.
    bool isCamFocused = false;

     [SerializeField] private KeyCode goBackToPlayerKey = KeyCode.Q;
     
     public void SwitchCamera()
     {
// Use Get key method to avoiding changeing cameras every frame when player push the key down and hold it there.
         if (Input.GetKey(goBackToPlayerKey))
         {
isCamFocused  = !isCamFocused ;
             Debug.Log("Switch?");
             playerCamera.gameObject.SetActive(isCamFocused ) ;
             artCamera.gameObject.SetActive(!isCamFocused ) ;

//I don't know what is this but FIND Method is a bad and expensive way to reference some asset so I //recommend you use serializing it(caching)
         GameObject originalGameObject = GameObject.Find("PaintingLarge");
         GameObject child1 = originalGameObject.transform.GetChild(0).gameObject;
         Debug.Log("Painting" + child1.name);

         }

void Update()
{
//you should call it here to be ready for it and don't use Key down to avoid errors and glisches when //user keeps key down this code will run accordingly 
SwitchCamera();
}

     }