- Home /
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;
}
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?
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;
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;
}
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?
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");
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.
Sorry, I feel like an idiot I didn't even see the small text under the questions.
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!
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.
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;
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" );