- Home /
Place object in different positions when L is clicked
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.
Why would the index become the remainder of a division of itself by the array length? Anyhow, there's nothing overtly wrong here, except quite possibly that. When posting questions, please specify what is happening, and what you want to happen ins$$anonymous$$d.
I'm thinking you're misusing arrays. Where do you want to input positions? In the inspector? I dunno squat about .js, but here's the reference page for working with .js Arrays. http://docs.unity3d.com/ScriptReference/Array.html
To pick the next position in the array, just increase the index by one. When posting error messages, please paste the entire error so that we can see the line number - the first number in a pair in parentheses. That'll also help you locate problems yourself.
I will convert it to an answer then, would be nice if you accept it :D Thanks in advance if you do :)
Answer by MrSoad · Nov 11, 2014 at 11:43 AM
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);
}
Your answer
Follow this Question
Related Questions
Linking array of sets of vector3's together 1 Answer
Is there any option to put objects together? 0 Answers
How to make flat object(Unity 2D) 1 Answer
Assigning to Vector3 array. 1 Answer
if close to one of a kind? 2 Answers