how to save positions in Vector3 Array und move playerobjekt to the saved position in array's??
Hi,
im wiriting a Ludo in 3D in C# in Unity 5. I want to save all the positions on gamefield where the player can move in a vector3 array. I did it so:
public class gameController : MonoBehaviour {
public List<Vector3> playgroundForRed;
void start () {
List<Vector3> playgroundForRed= new List<Vector3>();
// here i want to fill the arrayList with positions of gameobject I made for every position the player shall move (SFeld is the gameObject where the player can move)
rotesSpielFeld.Add(GameObject.Find("SFeld1").transform.position);
rotesSpielFeld.Add(GameObject.Find("SFeld1").transform.position);
rotesSpielFeld.Add(GameObject.Find("SFeld2").transform.position);
}
Then after the dice roll a 6 the player shall move out of his base to SFeld1 position: i did it like this in update():
GameObject.Find("PlayerR1").transform.position = rotesSpielFeld[0];
But this don't work. The player doesn't move.
I realy don't know what I have to do now..
On the otherside I tried to save every single position direct in Unity like this:
![alt text][2]
and
The player jump to the Green Base ![alt text][1]
(This position isn't saved in the vector3 array) - there is no position where he moved ^^ But the Player has to go to the first red field on the Playground.
But when I write
GameObject.Find("PlayerR1").transform.position = GameObject.Find("SFeld1").transform.position;
The Player move to the right position, but then I would have a problem on the nice dice roll. Thats why why I thought to save the positions in the array so i can "just" add the dice number to the next index.
I realy hope someone can help me..
Best Regards!!
Luke [1]: /storage/temp/69099-movementredplayer1.jpg [2]: /storage/temp/69100-fillledarraylistwithpositions1.jpg
public List<Vector3> playgroundForRed;
void Start(){
playgroundForRed= new List<Vector3>();
}
& you are adding elements to rotesSpielFeld
not playgroundForRed
Oh Sry I forget to translate - rotesSpielFeld, this is the original name i use, but i have translated for the question in "playgroundForRed". In the original code the Name is always "rotesSpielFeld".
No Sry, you misunderstood me. In the Code the name is always rotesSpielFeld - I just translated it here once and forgott the rest. So your answer was just a hint for wrong Translation here -.-
again : the last solution is fine & simple what problem you have with the dice ? & stop answering use comments click replay under my comments.
awesome :) Thank You!! The First problem is fixed. So next one with the dice is:
The Player is now on his startposition. with: GameObject.Find("Würfel").GetComponent().aktuellerWert
I can read the number from the dice, when he is rolled (1-6). How can i move now the player from his startPosition to the next arrayposition. I think the best way is with a loop, but i don't know how to create the loop.
u know what i mean?
i am doing the same & i am @ the last steep stay tuned
Answer by Ali-hatem · Apr 30, 2016 at 02:39 PM
so i made it with keyboard input you can edited as you want & the good news is all we need is one short script for all & don't worry values are different in each object since we assign values in inspector for each object :
int currint;
public List<Vector3> poslist = new List <Vector3>();// make 7 elements in inspector!
public int dice;
void Update(){
if(my turn){ //assuming you made something to detect turn
currint = poslist.FindIndex (d=>d == transform.position);
if(Input.GetKeyDown(KeyCode.Space)){
dice = Random.Range (1,6);
transform.position = poslist[currint + dice];
}
}
}
pay attention :
because arrays & lists start from 0 i made 7 elements & the zero will hold the main position of player so adjust targeted positions from 1 to 6 as desired .
you will face error if transform.position > poslist indexes i mean if you @ poslist[4] & the dice number is 4 player should go to poslist[8] which we don't have .
so i don't know what is the limitation of positions & what should happen if player reaches the end .
That's nice, I will try it in a few $$anonymous$$utes.
to be honest, I try to write a Ludo 3D game, so the playfield is 40 elements. I added them all in the vector3 arraylist. Like you said before.
rotesSpielFeld = new List<Vector3>();
rotesSpielFeld.Add(GameObject.Find("SFeld1").transform.position);
.....
rotesSpielFeld.Add(GameObject.Find("SFeld40").transform.position);
I will try it and wirte you back as soon as possible!
sorry it's my bad i didn't read the name of game "Ludo" but it's the same concept & you have to make end position to prevent more movements or destroy gameObject & add vectors in inspector it's visual & simple
if player position i.e it's vector didn't match any array elements it will be treated as -1 element so have you missed something & after it go to position 6 & the dice roll again what happen & i might replay tomorrow .