Switching Cameras (375175)

I want to have several cameras in a scene, each one activated by a trigger as a character or object passes by them (viewing the character from different angles), but I can’t find any references to this in the documentation. Could someone please point me in the right direction? I assume it’s a scripting thing, using GameObject.FindWithTag, but my attempts at activating or deactivating cameras just ended up making the cameras immobile (or mobile). Thanks in advance!

Edit: Oops, cameras are components, not game objects, but I couldn’t figure out how to access them through getComponent either.

I’m trying to do the same thing, Cameras have layers right? can layers be used to enable / disable the cam?

Quite possibly I’m not understanding what you’re trying to do exactly, or what the cameras are doing, but maybe something like this:

var cameraObject : GameObject;

function OnTriggerEnter () {
    cameraObject.active = true;
}

function OnTriggerExit () {
    cameraObject.active = false;
}

You’d make a cube (or some object) in Unity, and position and scale it where you wanted the trigger, then turn off the mesh renderer, and make sure “Is Trigger” is checked for the collider. Then attach the above script to it. Then drag the appropriate camera that the trigger should turn on and off onto the slot in the inspector for that script. So that camera will activate or deactivate when something enters or leaves that trigger…repeat with other triggers and cameras.

However, that’s pretty basic, so I suspect you’re trying something more complicated…are the cameras supposed to follow the object that enters the trigger maybe?

If that’s the case, do the same as above, but use this script instead:

var cameraObject : Transform; 

function OnTriggerStay (triggerObject : Collider) { 
	cameraObject.LookAt(triggerObject.transform.position);
}

Of course, you could combine the scripts and have the camera activate, then follow the object in the trigger, then deactivate.

–Eric

Thanks, Eric. Yes, that’s exactly what I was looking for. I’m still learning the js basics, and I had the active = true part right, but not the cameraObject part.

Thanks also for the second script. I combined it with the first, and it works much better. :slight_smile:

Oh, good. :slight_smile: “cameraObject” is, of course, a variable and you can call it whatever you want. Maybe something more generic like “theObject” would be better, since you could use those scripts for anything, not just cameras. For example, modify that a bit to turn scripts on and off instead of the entire gameObject, and you could have a missile launcher or gun that tracks the player. Then you’d just add a script to the gun to fire projectiles in whatever direction it happens to be facing.

–Eric

Like the free-standing guns in the 2nd FPS Tutorial. That’s cool, too. It’s amazing how many different things you can do with each function. Every time I learn something new, I have twenty ideas for how to use it.

(By the way, I’m using your light-beams-in-a-room example, from another thread, as a level in my test game. I showed it to a friend, and he was really impressed. In fact, I use one of the light beams as a trigger for entering another level, so I’ve turned it into a “transporter room” and changed the textures to make it look alien.)