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