Skip to main content
added 425 characters in body
Source Link

I am making a board game where the players hit a dice and move on an array of GameObjects according to the number they got.

So I declared a Gameobject array and added to it all the objects I need.

The problem is that when the player moves, he is going immediately to the target.

What I want is to make the player move through the Gameobject array, object by object, till it reaches the target(which is defined according to the dice number).

Here is what I am trying (using C#):

for(int i=0;i<spaces.Length;i++)
{
    transform.position = Vector3.MoveTowards(transform.position,
    spaces[currentPosition+diceResult].transform.position,Time.deltaTime / smooth );
}

spaces is the name of GameObject[] array.

currentPosition is the position of player before hitting the dice.

diceResult is the number he got in the dice.


EDIT :

I solved it!

Here is how it worked:

 if (index < spaces.Length)
 {
       transform.position = Vector3.MoveTowards(transform.position,
       spaces[index].transform.position, Time.deltaTime *smooth);

       //If we've reached the destination, move to the next one
       if (transform.position == spaces[index].transform.position) 
            index++;
 }

I am making a board game where the players hit a dice and move on an array of GameObjects according to the number they got.

So I declared a Gameobject array and added to it all the objects I need.

The problem is that when the player moves, he is going immediately to the target.

What I want is to make the player move through the Gameobject array, object by object, till it reaches the target(which is defined according to the dice number).

Here is what I am trying (using C#):

for(int i=0;i<spaces.Length;i++)
{
    transform.position = Vector3.MoveTowards(transform.position,
    spaces[currentPosition+diceResult].transform.position,Time.deltaTime / smooth );
}

spaces is the name of GameObject[] array.

currentPosition is the position of player before hitting the dice.

diceResult is the number he got in the dice.

I am making a board game where the players hit a dice and move on an array of GameObjects according to the number they got.

So I declared a Gameobject array and added to it all the objects I need.

The problem is that when the player moves, he is going immediately to the target.

What I want is to make the player move through the Gameobject array, object by object, till it reaches the target(which is defined according to the dice number).

Here is what I am trying (using C#):

for(int i=0;i<spaces.Length;i++)
{
    transform.position = Vector3.MoveTowards(transform.position,
    spaces[currentPosition+diceResult].transform.position,Time.deltaTime / smooth );
}

spaces is the name of GameObject[] array.

currentPosition is the position of player before hitting the dice.

diceResult is the number he got in the dice.


EDIT :

I solved it!

Here is how it worked:

 if (index < spaces.Length)
 {
       transform.position = Vector3.MoveTowards(transform.position,
       spaces[index].transform.position, Time.deltaTime *smooth);

       //If we've reached the destination, move to the next one
       if (transform.position == spaces[index].transform.position) 
            index++;
 }
Source Link

How can I make player move on array of GameObjects in Unity?

I am making a board game where the players hit a dice and move on an array of GameObjects according to the number they got.

So I declared a Gameobject array and added to it all the objects I need.

The problem is that when the player moves, he is going immediately to the target.

What I want is to make the player move through the Gameobject array, object by object, till it reaches the target(which is defined according to the dice number).

Here is what I am trying (using C#):

for(int i=0;i<spaces.Length;i++)
{
    transform.position = Vector3.MoveTowards(transform.position,
    spaces[currentPosition+diceResult].transform.position,Time.deltaTime / smooth );
}

spaces is the name of GameObject[] array.

currentPosition is the position of player before hitting the dice.

diceResult is the number he got in the dice.