- Home /
Main Camera Switch?
Hi Everyone, I've lately Been making an FPS Game, and There are two Cameras, One Camera is FPS and one is Third Person, I've seen Collider changes on other questions and I'm like What? So Is there a Way I Can Trigger a Camera Switch through a Binded Key?
Thanks everyone
Answer by Jason_DB · Apr 04, 2010 at 01:09 AM
You can activate and deactivate cameras if you can get a hold of the object that holds them.
//cameraObject is the gameObject which holds the camera
cameraObject.camera.active = false;
So If you had a script which kept track of all the cameras in your scene then you could switch cameras easily. For example here is a function which would go through an array of camera objects and turn a certain one on (based on the index sent to it).
var cameras : GameObject[];
function SelectCamera (index : int) { for (var i : int=0 ;i<wcameras.length; i++) { // Activate the selected camera if (i == index){ cameras[i].camera.active = true; // Deactivate all other cameras }else{ cameras[i].camera.active = false; }
}
You could use this function by calling it with a message
BroadcastMessage ("SelectCamera", 1);
or by outright calling it
SelectCamera(1);
You just need to get hold of the instance of the script it's in
Answer by Narven · May 02, 2010 at 06:40 AM
You can use something like
public Camera backCamera;
public Camera topDownCamera;
in the update...
if (Input.GetKey(KeyCode.Alpha1)) { driverCamera.camera.active = false; topDownCamera.camera.active = true; }
if (Input.GetKey(KeyCode.Alpha2))
{
topDownCamera.camera.active = false;
backCamera.camera.active = true;
}
}
Using this answer brought a loud smile on my face (that is, I vote Up, but can not - reputation issue). using UnityEngine; using System.Collections;
public class Fader : $$anonymous$$onoBehaviour {
public GameObject extraCamera;
void Start() {
Rect currentRes = new Rect(-Screen.width * 0.5f, -Screen.height * 0.5f, Screen.width, Screen.height);
guiTexture.pixelInset = currentRes;
extraCamera.camera.enabled = false;
}
}
Then I simply 'attached' (drag&drop) my secondary camera ($$anonymous$$$$anonymous$$ap) from Hierarchy to Inspector of 'Fader' GameObject.
ThumbsUp
Simon
Answer by Sebas · Apr 04, 2010 at 01:08 AM
Sure you can. The search function is your friend. See this Answer
If you combine this with pressing a key, you should be all set. If you want it to be more fancy and smoothly transition between both cameras, see this forum thread.