- Home /
Cycle through an array? Have Camera Follow different Targets.
I have a couple of characters,
I would like to do a mouse scroll and when so, have the camera change the target.
I browsed alot, couldent pice it together. Some help, please?! I am new to this program. Its been kind of hard learning on my own.
My initial Variables:
var targetArray : Transform;
var index : int = 0;
function Start()
{
playersList = GameObject.FindGameObjectsWithTag("Player");
}
Then I have This Mouse orbit script Focusing on a target
x += Input.GetAxis("Mouse X") * xSpeed * 0.01;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.01;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
//??? cycle ******************************************@!#!@%!%$@#!@
var position = rotation * Vector3(0.0, 0.0, -distance) + targetArray.position;
transform.rotation = rotation;
transform.position = position;
And the array
//cycle
if (Input.GetAxis("Mouse ScrollWheel")<0) //back
{
index=index-1;
}
if (Input.GetAxis("Mouse ScrollWheel")>0) //back
{
index=index+1;
}
if(index>2)
{
index=0;
}
if(index<2)
{
index=0;
}
targetArray = playersList[index].transform;
Comment
Best Answer
Answer by Maerig · May 28, 2014 at 05:31 AM
The main problem is
if(index<2)
{
index=0;
}
You just need to change the condition :
if(index<0)
{
index=0;
}
Then, you'll probably want to change your script so that when you scroll from the last elements, it's either blocked or it goes back to the first / last element :
if(index > playersList.Length-1)
{
index=playersList.Length-1; // index=0; if you want to rotate through the array
}
else if(index<0)
{
index=0; // index=playersList.Length-1; if you want to rotate through the array
}