Enable/disable cameras

Hey guys i have this script for hidding in a freezer

public class HIDE : MonoBehaviour
 {
// Start is called before the first frame update
public GameObject uiObject;
public GameObject uiUnhide;
public Animator Anim;
public bool InRange;
public bool IsHidding;
public Camera MainCamera;
public Camera HideCamera;
void Start()
{
    uiUnhide.SetActive(false);
    uiObject.SetActive(false);
    MainCamera.enabled = true;
    HideCamera.enabled = false;
}
void OnTriggerEnter(Collider player)

{
    if (player.gameObject.tag == "Player")
    {

        uiObject.SetActive(true);
        InRange = true;

    }
}
void OnTriggerExit(Collider Player)
{
    uiObject.SetActive(false);
    InRange = false;
    Anim = GetComponent<Animator>();
    Anim.SetBool("isOpen", false);
}

void Update()
{
    if (InRange == true && IsHidding == false)
    {
        if (Input.GetKeyDown(KeyCode.E))

        {
            Anim = GetComponent<Animator>();
            Anim.SetBool("isOpen", true);

            MainCamera.enabled = false;
            HideCamera.enabled = true;
            GameObject.Find("Player").SetActive(false);
            IsHidding = true;
            uiObject.SetActive(false);
        }

        if (IsHidding == true)

        {
            uiUnhide.SetActive(true);

            if (IsHidding == true && Input.GetKeyDown(KeyCode.E))

            {
                Anim = GetComponent<Animator>();
                Anim.SetBool("isOpen", true);
                HideCamera.enabled = false;
                MainCamera.enabled = true;
                GameObject.Find("Player").SetActive(true);
                IsHidding = false;
            }
        }

    }
}

}

the problem is that when i enter the freezer the hide camera (a camera attatched inside the freezer) is also deactivates at the first e and i have no cameras available …can you help me?

Ok… First of all I must address the fact that you’re not using cinemachine for the camera functionality. There’s this feature on cinemachine where you can attach an the virtual camera blends to an animator. It’s called the “State driven camera”.

Essentially allowing you to bind your camera’s position to what youre doing. I wish i could explain how that works but youre just gonna have to check out this beginner tutroial unity made: Using Cinemachine

Another thing i need to address is your over usage of that ishiding if statment.

What I’m seeing, is that your nesting contradicting if statments.
Your checking if the player is not hiding and if it is also hiding and you press the button to unhide. Think about that, it makes like no sense at all.

Here’s the version of the code I refactored. Make sure to read the comments, and if you want, delete them when you understand what I’m doing here.

public class HIDE : MonoBehaviour
  {
      
 public GameObject uiHide; //try to keep variables undertandable
 public GameObject uiUnhide;
 public Animator Anim;
 public Camera MainCamera;
 public Camera HideCamera;

 //I'm guessing this script is being stored on the fridge
 //in that case im storing the player here to make calls go down
 gameobject player;

 //these bools should only be accessable in this script
 bool InRange;
 bool IsHidding;

 // Start is called before the first frame update
 void Start()
 {
      //start is what initializes the component.
     Anim = GetComponent<Animator>();

     uiUnhide.SetActive(false);
     uiHide.SetActive(false);

     MainCamera.enabled = true;
     HideCamera.enabled = false;
 }
 void OnTriggerEnter(Collider collision) //your collider collides with more than just the player
 {
     if (collision.gameObject.CompareTag("Player")) //does the same thing you did but better
     {
         InRange = true;
     }
 }
 void OnTriggerExit(Collider collision)
 {
     if (collision.gameObject.CompareTag("Player")) //make sure the player is what's leaving the fridge
     {
         InRange = false;
     }     
     
 }
    void Update () {
        //if your are in range and are not hiding show the "hide" ui
        //otherwise if youre in range and are hiding show the "unhide" ui 
        uiUnhide.SetActive(IsHidding && InRange);
        uiHide.SetActive(!IsHidding && InRange); 

        //guessing that if your hiding the fridge, the door will be closed
        Anim.SetBool("isOpen", !IsHidding);

        //if the player is hiding show the hide camera
        //and if he's not hiding show the main camera
        MainCamera.enabled = !IsHidding;
        HideCamera.enabled = IsHidding;

        player.SetActive(!IsHidding); //if the player is hiding disable him
    }

 void OnTriggerStay(Collider collision) //if your still inside the fridge's collider
 {
    //if you are in range, and are not hiding, and you press E then youre now hiding. 
    
    IsHidding = InRange && input.GetKeyDown(KeyCode.E) && !IsHidding);
 }
}

I don’t know why you disable the player, but ok.

I must address one last thing: I did not test the code, you’ll have to try to debug it if it goes wrong.

I hope you don’t mind how much the code changed. Have a good day and i hope what your trying to make goes well.

  • Zak Kaioken