- Home /
how do i cycle back and forth through cameras in game.
Hi all,
I am currently using a script to cycle through my cameras in game so that when i hit the "x" key for instance it will deactivate the current camera and activate the next one in a list. The functionality i want to add to this is that I'd like to be able to go back through the cameras as well as forward. So for example the "x" key might cycle through from camera 1-5, but pressing "z" will allow you to cycle back through from camera 5-1. This is the code i am currently using:
var selectedCamera : int = 0;
function Start ()
{
SelectCamera (0);
}
function Update ()
{
if (Input.GetKeyDown("x"))
{
selectedCamera++;
if(selectedCamera == transform.childCount){
selectedCamera = 0;
}
SelectCamera (selectedCamera);
}
}
function SelectCamera (index : int)
{
for (var i=0;i<transform.childCount;i++)
{
if (i == index){
transform.GetChild(i).gameObject.SetActiveRecursively(true);
}
else{
transform.GetChild(i).gameObject.SetActiveRecursively(false);
}
}
}
Any help greatly appreciated.
Answer by tanoshimi · Nov 15, 2013 at 12:22 PM
How about adding the following to your Update():
if (Input.GetKeyDown("z"))
{
selectedCamera--;
if(selectedCamera < 0){
selectedCamera = transform.childCount;
}
SelectCamera (selectedCamera);
}
?
Thank you very much. That has done the trick.
thanks again
Glad to have helped. Please mark this as the answer so the board keeps tidy ;)
Your answer
Follow this Question
Related Questions
Directional wheel collider suspension? 0 Answers
Distinguish whether my Unity (Mac) is activated as Pro version or still in 30-day trial? 2 Answers
Enter Trigger activate Gui Texture 1 Answer
Platform moves when character steps on it 1 Answer
Inventory System- Best to Instantiate or Activate equipped items? 0 Answers