• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by alexkarak · Apr 08, 2019 at 03:35 PM · script error

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by zakkaiokenx10 · Apr 09, 2019 at 06:41 AM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

103 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Object flashing in and out every frame (because of my script) 0 Answers

Roll a Ball tutorial 1 Answer

First Person Controller Scripts Are Not Working! 1 Answer

transform.position assign attempt for not valid. Input position is { NaN, NaN, NaN } -1 Answers

day night script errors 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges