I am new to Unity and I am trying to do a 2d board game. In the game there is a dice and when It is player’s turn he will click on the dice and dice will chose a random number between 1-7. The player will move according to dice. I am using these scripts;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dice : MonoBehaviour
{
Rigidbody rb;
bool hasLanded;
bool thrown;
Vector3 initPosition;
public int diceValue;
public DiceSide[] diceSides;
public bool IsDoneRolling;
// Assign game object through editor
public GameObject gameControllerGameObject;
private GameController gameController;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
initPosition = transform.position;
rb.useGravity = false;
}
void Awake()
{
gameController = gameControllerGameObject.GetComponent<GameController>();
}
// Update is called once per frame
void Update()
{
if (rb.IsSleeping() && !hasLanded && thrown)
{
hasLanded = true;
rb.useGravity = false;
rb.isKinematic = true;
SideValueCheck();
gameControl.MovePlayer(diceValue);
}
else if (rb.IsSleeping() && hasLanded && diceValue == 0)
{
RollAgain();
}
}
public void RollDice()
{
if (!thrown && !hasLanded)
{
IsDoneRolling = false;
thrown = true;
rb.useGravity = true;
rb.AddTorque(Random.Range(0, 250), Random.Range(0, 250), Random.Range(0, 250));
}
else if (thrown && hasLanded)
{
Reset();
}
}
void Reset()
{
transform.position = initPosition;
thrown = false;
hasLanded = false;
rb.useGravity = false;
rb.isKinematic = false;
IsDoneRolling = true;
}
void RollAgain()
{
Reset();
IsDoneRolling = false;
thrown = true;
rb.useGravity = true;
rb.AddTorque(Random.Range(0, 250), Random.Range(0, 250), Random.Range(0, 250));
}
void SideValueCheck()
{
diceValue = 0;
foreach (DiceSide side in diceSides)
{
if (side.OnGround())
{
diceValue = side.sideValue;
Debug.Log(diceValue + " has been rolled!");
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerPiece : MonoBehaviour
{
public Route currentRoute;
int routePosition;
// Remove unnecessary variables
public bool isMoving;
// Update is called once per frame
void Update()
{
}
// Make this public so we can call it from GameControl
// Add number of steps to move as parameter
public IEnumerator Move(int steps)
{
if (isMoving)
{
yield break;
}
isMoving = true;
while (steps > 0)
{
Debug.Log("Route position: " + routePosition);
routePosition++;
routePosition %= currentRoute.childNodeList.Count;
Vector3 nextPos = currentRoute.childNodeList[routePosition].position;
while (MoveToNextNode(nextPos)) { yield return null; }
yield return new WaitForSeconds(0.1f);
steps--;
}
isMoving = false;
}
bool MoveToNextNode(Vector3 goal)
{
return goal != (transform.position = Vector3.MoveTowards(transform.position, goal, 8f * Time.deltaTime));
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameControl : MonoBehaviour
{
// Be careful about using static variables when they're not necessary
private GameObject player1, player2;
// Add references to the player piece scripts
private PlayerPiece player1Piece;
private PlayerPiece player2Piece;
private whosTurn;
// Start is called before the first frame update
void Awake()
{
player1 = GameObject.Find("Player1-Piece");
player2 = GameObject.Find("Player2-Piece");
// Set the reference to the script so you don't have to call GetComponent() each time
player1Piece = player1.GetComponent<PlayerPiece>();
player2Piece = player2.GetComponent<PlayerPiece>();
}
// Update is called once per frame
void Update()
{
// Clean this out and we'll handle movement directly in the PlayerMove
}
// Change variable to handle steps to move
public void MovePlayer(int steps)
{
switch (whosTurn)
{
case 1:
StartCoroutine(player1Piece.Move(steps));
break;
case 2:
StartCoroutine(player2Piece.Move(steps));
break;
}
whosTurn *= -1;
}
}
When I came back to Unity It says there is an error on GameControl.cs (the 3rd one) 13th line. Also the scripts Named Dice.cs , FollowThePath.cs , GameObject.cs respectively
I tried to move a pawn according to a dice. I was expecting when I click on dice it will give a random number and the pawn will move according to number. But all of them didnt happen. I just get some errors
Hee jj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.