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 newnixon · Sep 20, 2012 at 01:50 PM · camerachildtarget

How to enable a childs camera

If you guys could help that would be great. I have a plane game I am working on and I have a camera that is attached to the plane that looks for the closest target once the plane is in the air. I have code that also uses the scroll wheel to zoom into the target. The problem is the plane is so high that I can't get a closer zoom of the targets to see what they are. So I put a camera on the targets so that when the max of the scroll wheel is reached the camera of he target will come on. So now I can't get the camera to be enabled on the targets. I can find the game object that is the closest but I can't figure out how to turn the camera on because it is a child of the target.

 var speed : float ;
 var turn : float;
 var a=100;
 var b=2;
 var mcamera:Camera;
 var myTarget: GameObject;
 
 
  function Update()  {
 
  if (Input.GetAxis("Mouse ScrollWheel") > 0 && mcamera.fieldOfView > b)
 
 {
  mcamera.fieldOfView--;
  }
 
 if (Input.GetAxis("Mouse ScrollWheel") < 0 && mcamera.fieldOfView < a)
  {
   mcamera.fieldOfView++;
  }
  
 FindClosestEnemy();
 //print(FindClosestEnemy().name); 
 myObject = FindClosestEnemy().name; 
 
   if(mcamera.fieldOfView == 0){
   
  print ("field of view is at zero");
  //This is where I want the camera of the target to come on.
     
  }
  }
  function FindClosestEnemy () : GameObject {
     // Find all game objects with tag target
     var gos : GameObject[];
     gos = GameObject.FindGameObjectsWithTag("target"); 
     var closest : GameObject; 
     var distance = Mathf.Infinity; 
     var position = transform.position; 
      
     // Iterate through them and find the closest one
   
     for (var go: GameObject  in gos)  { 
         var diff = (go.transform.position - position);
         var curDistance = diff.sqrMagnitude; 
         if (curDistance < distance) { 
             closest = go; 
             distance = curDistance; 
            
             myTarget  = closest;
             //Here I can print to find the target just need to know how to turn on the camera
              print(myTarget);
           
         } 
         
     } 
       
       
      transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(closest.transform.position - transform.position), turn*Time.deltaTime);
    transform.position += transform.forward*speed*Time.deltaTime;
    return closest;
 }
 
Comment
Add comment · Show 1
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 MountDoomTeam · Sep 20, 2012 at 03:43 PM 0
Share

the plane is so high that you cannot put a child object of the plane that always stays at 200 meters and attach the camera to that, and then detect what's closest to it?

5 Replies

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

Answer by newnixon · Sep 21, 2012 at 12:29 PM

Thanks guys for your help this worked!!

myTarget.transform.Find("Camera").GetComponent(Camera).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
avatar image
-1

Answer by newnixon · Sep 20, 2012 at 03:32 PM

I am using JavaScript so I think it should be like this. myTarget.GetComponent(Camera).enabled = true;

With this it should just enable the the camera of the closest target to turn on but I get an error.

NullReferenceException: Object reference not set to an instance of an object

  function FindClosestEnemy () : GameObject {
     // Find all game objects with tag Enemy
     var gos : GameObject[];
     gos = GameObject.FindGameObjectsWithTag("target"); 
     var closest : GameObject; 
     var distance = Mathf.Infinity; 
     var position = transform.position; 
      
     // Iterate through them and find the closest one
   
     for (var go: GameObject  in gos)  { 
         var diff = (go.transform.position - position);
         var curDistance = diff.sqrMagnitude; 
         if (curDistance < distance) { 
             closest = go; 
             distance = curDistance; 
            
            myTarget  = closest;
             
            //  print(myTarget);
          myTarget.GetComponent(Camera).enabled = true;
         } 
         
     } 
        //return closest;   
      transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(closest.transform.position - transform.position), turn*Time.deltaTime);
    transform.position += transform.forward*speed*Time.deltaTime;
    //return closest;
 }
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 justinl · Sep 20, 2012 at 05:20 PM 1
Share

please update your original question with your new code ins$$anonymous$$d of pasting it as an answer (since it's not an answer). Also, what line in your code is giving you that NullReferenceException?

avatar image
-1

Answer by newnixon · Sep 20, 2012 at 06:28 PM

Guys thanks for you help but I am getting errors with this. I can't find anything with FindChild.

 Transform childCamTransform = myTarget.FindChild("yourChildCameraName");
Comment
Add comment · Show 4 · 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 justinl · Sep 21, 2012 at 01:33 AM 0
Share

newnixon, this is NOT a forum. This is a Q&A site. Do not post random stuff as new answers. Post discussion as comments. Also, you never got back to any of my questions so I cannot further assist you.

avatar image newnixon · Sep 21, 2012 at 11:57 AM 0
Share

Sorry, I feel like an idiot I didn't even see the small text under the questions.

avatar image MountDoomTeam · Sep 21, 2012 at 01:10 PM 0
Share

yeah i didnt see them either because i had high contrast enabled! i got -4 i was depressed. ... yeah you have to think a while before asking a question, and normally if you ask a question clearly people here will save your life and help invaluably!

avatar image justinl · Sep 21, 2012 at 01:59 PM 0
Share

Hey Newnixon, I'd recommend using GameObject.Find("Camera").GetComponent(Camera).enabled = false; ins$$anonymous$$d. You're using your child camera to find the parent camera, and there's really no reason for it since all you're doing is finding your main camera reference and turning it OFF, (thus allowing your child camera to display). Using your child GameObject to find an arbitrary object in the scene isn't good practice and could be confusing to you later when you come back to the code to read it.

Lastly, please check one of the answers as correct or edit your answer with your solution and mark that as correct. That way, this question will not sit in the "Unanswered Questions" pool.

avatar image
0

Answer by justinl · Sep 20, 2012 at 02:28 PM

I believe this is what you're looking for to control your camera:

 myTarget.GetComponent<Camera>().enabled = true;
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
avatar image
0

Answer by Sundar · Sep 20, 2012 at 04:05 PM

try this

  Transform childCamTransform = myTarget.FindChild("yourChildCameraName");
 
 if( childCamTransform ) childCamTransform.camera.enabled = true;
 else print( "No child camera found for myTarget" );
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

12 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

Related Questions

Moving Camera With 2 Players 3 Answers

how to get the child of an instanteated object 2 Answers

Child affecting parent rigidbody? 1 Answer

Camera focusing on an object specified in a script 1 Answer

How to set child position and rotation fixed respect parent? 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