How would you move from a array like from [2] to [3] or backwards like [2] to [1]
So lets say I am on example[3] I want to iterate to example[2] and only when a certain condition is met it will count down to array [2] but if I met a different condition it will count up to example[4] this is what I want to do so I can have 360 degrees coverage by my main camera on my player . help? I will post if I manage to solve my self
using UnityEngine; using System.Collections;
public class CameraController : MonoBehaviour
{
public GameObject player;
private Vector3 offset;
Vector3[] bawb = new Vector3[4];
void Start()
{
offset = transform.position - player.transform.position;
bawb[1] = new Vector3(10, 10, 10);
bawb[2] = new Vector3(20, 20, 20);
bawb[3] = new Vector3(30, 30, 30);
bawb[4] = new Vector3(40, 40, 40);
//camera positions(^ are test vectors)
}
void Update()
{
if (Input.GetKeyDown("q"))
{
//condition to interate a array number up
}
else
{
if (Input.GetKeyDown("e"))
{
//condition to interate a number down
}
}
}
void LateUpdate()
{
transform.position = player.transform.position + offset;
//subtracting vectors to get preferd camera angle(no touchy >:( )
}
}
what do you mean by iterating? once you have an array, you can loop through it, or simply use any of its values anytime.
First of all, please format your code properly. If you don't know how, have a look at the user guide of UnityAnswers.
Second your question is extremely cryptic and contains many errors. First of all an array with 4 elements has the valid indices of 0, 1, 2 and 3. So in your case you never set the first element and when you try to set the element with index "4" you'll get an out-of-bounds exception.
The next strange things is your vectors are positions and you have arranged them in a line, yet you talk about rotation and 360° which makes no sense. Position and rotation are two completely different things.
Ins$$anonymous$$d of talking about array iterating up and down when some abstract conditions are met, you should simply describe as detailed as possible what you want as a result. You just explained a small part of your attempt but the actual problem is still unknown. Your code doesn't use your array anywhere so how should we deduct what you want to do?
Answer by UnityCoach · Dec 11, 2016 at 08:08 AM
for (int i = 10 ; i > 5 ; i--) // will go from 10 to 6 by 1
{
Debug.Log(theArray[i]);
}
for (int i = 10 ; i < 90 ; i+=10) // will go from 10 to 90 by 10
{
Debug.Log(theArray[i]);
}
Answer by dpoly · Dec 11, 2016 at 09:04 AM
The form of the question is pretty hard to understand, but with a few guesses...
The index into an array is an integer. To 'iterate up' increment the integer. To 'iterate down' decrement it.
// make this an instance variable for the class
private int indx = 3; // index for array[3]
// these can go inside any method of the class
indx++; // increment
indx--; // decrement
If you go outside the range of the array, you will get a bounds error. So don't. You need to add code to check for that.
Ok, i'm sorry for my Code post being confusing, I reformatted it so it would appear clear and I changed what I want my main camera to do to be clearer(witch is btw so that I can see from all sides of the player like 360 degree coverage so my game doesn't have to be a 2d platformer
Anyway, I can't seem to do bawb++ becuase it says I can't use a local variable before its declared?
@dragonking300: You're asking really basic, beginner C# questions. You should probably take a break and go do a month or two working through C# tutorials before asking Unity questions. But see edit.