Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by DariuszUK · May 30, 2021 at 07:36 AM · scripting problemgameobjectraycastswitch

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 :(

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.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SadeqSoli · May 31, 2021 at 03:48 PM

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





Comment
Add comment · Show 1 · 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
avatar image DariuszUK · Jun 07, 2021 at 10:10 PM 0
Share

Hi, Thank you for your input. It is not exactly what I had in $$anonymous$$d but I will make the adjustments to your comments. With the Find function, I am planning on having 80-120 objects (created from prefab, each prefab would have a model, the camera inside and above script to interact with - if that make sense) Given the amount of manual labour where I would have to physically drag each camera to specific field I wanted to automate it a little bit. Thank you for the help so far!

avatar image
0

Answer by Statement · May 31, 2021 at 09:42 AM

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;
          }
 }
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.

Follow this Question

Answers Answers and Comments

295 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 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 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

How to develop a script that substitute a gameobject position with my character? 0 Answers

i am making an increment of one when user clicks a game object, trying to decrement the counter if the same game object is clicked again 1 Answer

Trying to get a VR Gun Raycast to work 1 Answer

I need help with raycasts and with color of gameobject [SOLVED] 3 Answers

See what object I hit in raycast 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