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:

69100-fillledarraylistwithpositions1.jpg

and

The player jump to the Green Base 69099-movementredplayer1.jpg

(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

public List<Vector3> playgroundForRed; void Start(){ playgroundForRed= new List<Vector3>(); } & you are adding elements to rotesSpielFeld not playgroundForRed

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 -.-

Hey, thank you for your comment. I don't have a problem with the fact that it lags when my laptop is not plugged in, but I do have a problem that my game lags on mobile phones..... Please could you have a look at this asset: https://www.assetstore.unity3d.com/en/#!/content/59187 When you play it on mobile, it LAGS.... Why is this? It is only a SIMPLE 2D mobile game? Where games like the real color switch run completely smoothly?

1 Answer

1

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 :

  1. 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 .
  2. 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 .
  3. 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 minutes. 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!