Anyone know how to switch cameras with the mouse wheel instead of pushing keys. I would like for the player to have to scroll up and down to switch to different cameras. For example, scrolling up takes you to camera 4 but you have to scroll down to switch to camera 3, camera2, camera1 and vice versa.
The cameras are children to the FPS controller and I am using Unity 1.6.2
Any help would be appreciated.
Thanks
Here’s one way:
var cameras : Camera[];
private var activeCamera : int = 0;
function Update ()
{
var axis : float = Input.GetAxis ("Mouse ScrollWheel");
if (axis > 0.0)
{
if (activeCamera == cameras.length - 1)
{
activeCamera = 0;
}
else
{
activeCamera += 1;
}
}
else
if (axis < 0.0)
{
if (activeCamera == 0)
{
activeCamera = cameras.length - 1;
}
else
{
activeCamera -= 1;
}
}
for (i = 0; i < cameras.length; i++)
{
if (activeCamera == i)
cameras [i].enabled = true;
else
cameras [i].enabled = false;
}
}
Hey Dan thanks for the script
Now I am trying to write the script to solve the other problem in my post. Here is the code:
var cameras : Camera[];
private var activeCamera : int = 0;
function Start()
{
activeCamera = 1; //this is so my second camera is the default camera when game starts, which is element 1 in the array
}
function Update ()
{
var axis : float = Input.GetAxis ("Mouse ScrollWheel");
if (axis > 0.0)
{
if (activeCamera == cameras.length - 1)
{
activeCamera = 0;
}
else
{
activeCamera += 1;
}
}
else
if (axis < 0.0)
{
if (activeCamera == 0)
{
activeCamera = cameras.length - 1;
}
else
{
activeCamera -= 1;
}
}
//Here I am trying to program it so that the player has to scroll the opposite direction once they reach one end of the array
if(axis > 1.0)
{
activeCamera = 0;
}
else if (axis <-1)
{
activeCamera = 2;
}
for (i = 0; i < cameras.length; i++)
{
if (activeCamera == i)
cameras [i].enabled = true;
else
cameras [i].enabled = false;
}
}
The problem now is that it won’t scroll through the array, just the first and last element of the array.
function Start()
{
activeCamera = 1; //this is so my second camera is the default camera when game starts, which is element 1 in the array
}
//Here I am trying to program it so that the player has to scroll the opposite direction once they reach one end of the array
if(axis > 1.0)
{
activeCamera = 0;
}
else if (axis <-1)
{
activeCamera = 2;
}
The code above is what I added in order to have default camera at the start of the game with the ‘function Start()’. That works.
The ‘if()’ statement, I am triyng to program it so the player can’t just scroll in one direction to go through the different cameras in the array. Once they reach one end of the array the player must scroll in the other direction to select the different cameras.
Now the above code is somewhat working in that I have to scroll in both directions, but it is only going to the first and last element of the array and nothing in between. I don’t know how to fix it.
Any help would be appreciated.
Thanks