# is using list of vector3 the best method for boardgame movement?

Ive seen a few forums but very little material on the style im looking for, i dont want the player to just move forward through the list. I want there to be interesections where you chose a direction, even if you want to go back the way you came. and the moment you do that things become super complex so im hoping someone will be kind enough to even just point me in the right direction. ive been stuck on this for a whlle. Ive already made rolling dice but i made a simple script version to base this concept on. i basically want that when you roll a 4(or whatever) the player is awarded the ability to move essentially 4 unity on the path. but i get lost when the idea that it wont be straight lines in any axis so i cant just increment my way through it i need what im guessing is a list of vector3

so i made a little code snippet as a bases, im still trying to wrap my head around c# so forgive my dumbness

``````using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SimpleTest : MonoBehaviour
{
public int diceAmount = 0;
public int amountMoved = 0;

private List<Vector3> Rolls = new List<Vector3>(); //<im guessing

// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
diceAmount = Random.Range(1, 6);
}

}

//im assuming i need some sort of for loop for each roll but i dont know how to properly call it
}
``````

Okay, so there’s a few things you seem to be asking(assuming you’re doing a Mario Party style board). First, for the visual locations of the “playspaces” you probably should just have scene objects with a PlaySpace component on them. The physical locations should only affect the art and not any of the intersections logic.

For the gameplay logic, there are several different ways you can move forward depending on your plans for the game. One way would be to just to have each playspace have a link to the nodes it can reach from there. In most cases you’ll have just 1(or 2 if you include backward movement). For intersections you’ll have more possible nodes. In order to keep the player going in the same direction, you can store the previous location the player was on, and make sure that when they move forward they choose a different space than where they were. Then you just do a loop for the dice roll to move forward one space at a time(with some sort of special logic for intersections with more than one possible location).

Sample Code:

``````using UnityEngine;
using System.Collections;
using System.Collections.Generic;

class PlaySpace : MonoBehaviour
{
public List<PlaySpace> connectedSpaces = new List<PlaySpace>();
};

public class PlayerToken : MonoBehaviour
{
// Assign to starting tile in editor!
public PlaySpace currentTile;

// Keep track of where you came from so you can backtrack. As per NewPath's suggestion, a Stack will let you backtrack more than 1 space in the case of an intersection.
public Stack<PlaySpace> travelledTiles = new Stack<PlaySpace>();

// How many squares you need to move
public int numSpacesToMove = 0;

// This is just a placeholder for movement animation. You can change this however you like
public float movementSpeed = 1.0f;

// How far in the move animation you are
private float movementProgress = 0.0f;

// The last dice roll, you probably don't need to store this except for UI hooks
public int diceAmount = 0;

// Are we waiting at an intersection?
public bool atIntersection = false;

void Update()
{
// Really, you only want to do this when you are finished moving
// i.e. after all interesection logic is done and the animation has completed
// That's why I added numSpacesToMove == 0
if (Input.GetButtonDown("Fire1") && numSpacesToMove == 0)
{
diceAmount = Random.Range(1, 6);
numSpacesToMove += diceAmount;
}

if (atIntersection)
{
// Some sort of choice input here!
}

// Update movement
if (numSpacesToMove != 0 && !atIntersection)
{
// Do animation for movement. (movementProgress is the animation progress toward reaching the next tile. You'd probably want to replace it with waiting for real animations at some point.)
movementProgress += movementSpeed * Time.deltaTime;
// Try to find the next square to go to in this direction
PlaySpace nextSquare = FindNextSquare();

// Depending on how fast the animation is, we may have reached multiple new tiles
while(nextSquare != null && movementProgress >= 1.0f && numSpacesToMove != 0)
{
movementProgress -= 1.0f;
if (numSpacesToMove < 0)
{
// If moving backward, we just remove the last tile and set ourselves there
currentTile = travelledTiles.Pop();
numSpacesToMove++;
}
else
{
// If moving forward, we need to remember where we were in case we move backward later, as well as to limit our options for moving forward.
travelledTiles.Push(currentTile);
currentTile = nextSquare;
numSpacesToMove--;
}

// If we haven't reached the end of the squares we're trying to move. Find the next one in the list
if (numSpacesToMove != 0)
nextSquare = FindNextSquare();
else
nextSquare = null;
}

// If we're at either an end point or a decision point, we stop at this tile
if (nextSquare == null)
{
if (!atIntersection && numSpacesToMove != 0)
{
// If we're not at an intersection, we must have run out of spaces to go!
Debug.Log("Dead end at " + currentTile.name + " had " + numSpacesToMove + " squares left!");
numSpacesToMove = 0;
}
movementProgress = 0.0f;
transform.position = currentTile.transform.position;
}
else
{
// Simple interpolation for when we are between two tiles
transform.position = Vector3.Lerp(currentTile.transform.position, nextSquare.transform.position, movementProgress);
}
}
}

// Figure out where we're going and where we came from!
PlaySpace FindNextSquare()
{
PlaySpace prevSquare = null, nextSquare = null;
if (travelledTiles.Count > 0)
prevSquare = travelledTiles.Peek();    // Look at the last tile you were at.

// If going backwards, we don't have a choice
if (numSpacesToMove < 0)
nextSquare = prevSquare;
else
{
// Here we keep a list of all tiles we can go to from our current tile, excluding the previous square
List<PlaySpace> availableSquares = new List<PlaySpace>();
foreach(PlaySpace space in currentTile.connectedSpaces)
{
if(space != prevSquare)
}

// If we have exactly one square, then we can keep moving
if (availableSquares.Count == 1)
nextSquare = availableSquares[0];
// But if we are at an intersection, we have to stop movement and wait for intersection input
else if (availableSquares.Count > 1)
{
atIntersection = true;
return null;
}
}
return nextSquare;
}
}
``````

Based on your comments, I believe for the board tiles you want a simple undirected graph structure. Each node would look something vaguely like this:

``````public class Node
{
public Transform Position { get; set; }
public ICollection<Node> Nodes { get; set; }
}
``````

This handles decisions about intersections easily, if there’s only one link don’t give them an option, if there’s more than one, let them chose. To hold the player’s movements, I would use a standard stack (found in System.Collections.Generic):

``````var movements = new Stack<Node>();
``````

So as the player traverses the graph, push each node on the stack. If they want to backtrack, you just pop the stack however many times.