changing aboardgame like linear array path to a have intersections where you choose left or right
thanks to the internet I have a working boardgame mechanic that moves the player along the route i assign, but i want there to be intersections where you can go left or right. I cant for the love of me wrap my brain around how i would do it... i feel like i would need two methods that would get called from OnClick() from two icons, im picturing a left and right icon that would update or replace the currentRoute position, any help would be awesome. this is the movement script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Stone : MonoBehaviour {
public Route currentRoute;
int routePosition;
public int steps;
public bool isMoving;
public int RoutePosition { get => routePosition; set => routePosition = value; }
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && !isMoving)
RollThemDice();
}
IEnumerator Move()
{
if (isMoving)
{
yield break;
}
isMoving = true;
while (steps > 0)
{
Vector3 nextPos = currentRoute.childNodeList[RoutePosition + 1].position;
while (MoveToNextNode(nextPos)) { yield return null; }
yield return new WaitForSeconds(0.1f);
steps--;
RoutePosition++;
}
isMoving = false;
}
bool MoveToNextNode(Vector3 goal)
{
return goal != (transform.position = Vector3.MoveTowards(transform.position, goal, 2f * Time.deltaTime));
transform.position = Vector3.MoveTowards(transform.position, goal, 2f * Time.deltaTime);
}
public void RollThemDice()
{
steps = Random.Range(1, 7);
Debug.Log("Dice rolled" + steps);
if (RoutePosition + steps < currentRoute.childNodeList.Count)
{
StartCoroutine(Move());
}
else
{
Debug.Log("rolled Number is to high");
}
}
}
Answer by rhapen · Oct 24, 2020 at 06:53 AM
if i get it right your question you can add your choices "left" "right" into the list where left will be just infront the right. Then on click method you assign bool right = true or if left then false
while (steps > 0)
{
int chosenPosition = RoutePosition + 1;
if(right) chosenPosition = RoutePosition + 2;
Vector3 nextPos = currentRoute.childNodeList[chosenPosition].position;
while (MoveToNextNode(nextPos)) { yield return null; }
yield return new WaitForSeconds(0.1f);
steps--;
RoutePosition++;
}
Thanks for the reply! full disclousre im pretty new to coding so forgive me, but are you saying you essential skip a tile, so if it went Tile1> Tile2> Tile3> (Intersection) Tile4 is Left and Tile5 is Right?
then does the right path go Tile5> Tile6> Tile7> while the left path is Tile4> Tile8> Tile9>. Hopefully im explaining that right cause thats where my problem lies
For me, the path will look more like a tree branch than a boardgame so there will be mutliple intersection and even reconnecting at further junctions.
I think you got the right answer but i got the wrong foundation to pull it off, I saw a method where the position of the next tile was individually stored in the previous tile and not through a massive list of Positions, it seems like trying to do multiple paths through one list is Counter productive. Arrays have been something ive struggled to understand from the get go, i really appreciate the answer tho its still very helpful to me i think i may have to take this one back to the beginning
yes you are right you have better go with something like a Dictionary https://www.tutorialsteacher.com/csharp/csharp-dictionary Where you can store key(maybe int or string) and value (Vector3) or maybe other multidimensional array where you can store the choices in one key that way you can call the direction on demand based on board position.