- Home /
how can I program for a character to be able to decide whether or not to take a path in an intercession, when moved by a dice from 1 to 6?
hi, I'm making a board game, I've created 2 scripts, where it worked well without errors, when I press space, the character moves based on a random number from 1 to 6 on a table, like a square with a line on de midle where they walk on it based on a dice movementbut, but the character only walks on the single path, without taking the midle path. I want that when the character comes to an intercession he can choose to continue on the path or go on the other way..(decide if he wants to turn or not). also want to associate de sprites of the dice with the correct random number, This is the script for "Stone" (the character):
public Route currentRoute;
int routePosition;
public int steps;
bool isMoving;
void Update()
{
if(Input.GetKeyDown(KeyCode.Space) && !isMoving)
{
steps = Random.Range(1, 7);
Debug.Log("Dice Rolled " + steps);
StartCoroutine(Move());
// if(routePosition+steps < currentRoute.childNodeList.Count)
// {
// StartCoroutine(Move());
// }
// else
// {
// Debug.Log("Rolled Number is to high");
// }
}
}
IEnumerator Move()
{
if (isMoving)
{
yield break;
}
isMoving = true;
while(steps>0)
{
routePosition++;
routePosition %= currentRoute.childNodeList.Count;
Vector3 nextPos = currentRoute.childNodeList[routePosition].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, 4f * Time.deltaTime));
}
And here for the "nodes" (the "nodes" are the spaces on the board where the character will be moving on acording to de dice numbers.):
void OnDrawGizmos()
{
Gizmos.color = Color.green;
FillNodes();
for (int i = 0; i <childNodeList.Count; i ++)
{
Vector3 currentPos = childNodeList[i].position;
if(i>0)
{
Vector3 prevPos = childNodeList[i - 1].position;
Gizmos.DrawLine(prevPos, currentPos);
}
}
}
void FillNodes()
{
childNodeList.Clear();
childObjects = GetComponentsInChildren<Transform>();
foreach(Transform child in childObjects)
{
if(child != this.transform)
{
childNodeList.Add(child);
}
}
}