I’m trying to make an array of positions so once L is clicked, it goes to position (12, 3, 4) then when I click L again I can make it go to (7, 3, 5).
var position : Vector3 = transform.position;
private var index : int;
function Update () {
if(Input.GetKeyDown("l")){
index++;
index = index % position.Length;
transform.position = position[index];
}
}
I’m inexperienced with javascript so it may be something obvious :s
Edit: It says "move.js(8,34): BCE0022: Cannot convert ‘float’ to ‘UnityEngine.Vector3’. What I want it to do is to let me choose how many different positions I want (The size) and let me input the positions into each one. Thus everytime I click L, it picks the next position in the array.
If you only want the two different positions then you do not really need an array, but below is how you would do what I think you want using an array(I’ve not tested this code but I think I’ve written it right).
//Decalare your Positions Array.
private var aPositions : Vector3[];
//Your switch position flag var.
private var bSwitch : boolean = false;
function Start() {
//Set the size of your positions array, 2.
aPositions = new Vector3[2];
//Fill your positions array with your two positions
subFill_Array();
}
function Update () {
//If "l" key pressed switch position.
if (Input.GetKeyDown("l")) {
if (bSwitch) {
transform.position = aPositions[0];
bSwitch = false;
} else {
transform.position = aPositions[1];
bSwitch = true;
}
}
}
function subFill_Array() {
aPositions[0] = Vector3(12, 3, 4);
aPositions[1] = Vector3(7, 3, 5);
}