How to make the 2D board game piece move only when the player answers the question correctly?
Hello, so I am creating 2D educational board game where player's figure moves if player answer given question correctly and if no he stay in same position. I allready done that my figure moves if player answer is correct but this only works when throwing the dice for the first time, when player roll dice second time figure do not wait for correct answer and moves immediately after dice roled. So maybe someone could help me and say how to make player's figure wait for correct answer not only for the first time but and for the next time when the dice is rolled? The game was created for two players but now I uploading script only for one player to make it easier to understand the code. Since I am new to working with Unity and C# so I apologize if my question may seem stupid, but I would be very grateful if you could help me guys.
Here is my game control script:
public class GameControl : MonoBehaviour {
private static GameObject whoWinsTextShadow, player1MoveText;
private static GameObject player1;
public static int diceSideThrown = 0;
public static int player1StartWaypoint = 0;
public static bool gameOver = false;
void Start () {
whoWinsTextShadow = GameObject.Find("WhoWinsText");
player1MoveText = GameObject.Find("Player1MoveText");
player1 = GameObject.Find("Player1");
player1.GetComponent<FollowThePath>().moveAllowed = false;
whoWinsTextShadow.gameObject.SetActive(false);
player1MoveText.gameObject.SetActive(true);
}
public void Update()
{
if (player1.GetComponent<FollowThePath>().waypointIndex >
player1StartWaypoint + diceSideThrown)
{
player1.GetComponent<FollowThePath>().moveAllowed = false;
player1StartWaypoint = player1.GetComponent<FollowThePath>().waypointIndex - 1;
}
if (player1.GetComponent<FollowThePath>().waypointIndex ==
player1.GetComponent<FollowThePath>().waypoints.Length)
{
whoWinsTextShadow.gameObject.SetActive(true);
player1MoveText.gameObject.SetActive(false);
whoWinsTextShadow.GetComponent<Text>().text = "Player win";
gameOver = true;
}
}
public static void MovePlayer()
{
player1.GetComponent<FollowThePath>().moveAllowed = true;
}
}
Here is my dice rolling script. Queston (canvas) shows when the dice button is clicked:
public class Dice : MonoBehaviour {
private static GameObject QuizCanvas;
private Sprite[] diceSides;
private SpriteRenderer rend;
private int whosTurn = 1;
private bool coroutineAllowed = true;
private void Start () {
QuizCanvas = GameObject.Find("QuizCanvas");
QuizCanvas.gameObject.SetActive(false);
rend = GetComponent<SpriteRenderer>();
diceSides = Resources.LoadAll<Sprite>("DiceSides/");
rend.sprite = diceSides[5];
}
private void OnMouseDown()
{
if (!GameControl.gameOver && coroutineAllowed)
{
QuizCanvas.gameObject.SetActive(true); // Show question when dice is clicked
StartCoroutine("RollTheDice");
}
}
public IEnumerator RollTheDice()
{
coroutineAllowed = false;
int randomDiceSide = 0;
for (int i = 0; i <= 20; i++)
{
randomDiceSide = Random.Range(0, 6);
rend.sprite = diceSides[randomDiceSide];
yield return new WaitForSeconds(0.05f);
}
GameControl.diceSideThrown = randomDiceSide + 1;
if (whosTurn == 1)
{
GameControl.MovePlayer();
}
coroutineAllowed = true;
}
}
Here is my script for piece movement:
public class FollowThePath : MonoBehaviour {
public QuizManager quizManager;
public GameControl gameControl;
public Transform[] waypoints;
[SerializeField]
private float moveSpeed = 1f;
[HideInInspector]
public int waypointIndex = 0;
public bool moveAllowed = false;
private void Start () {
transform.position = waypoints[waypointIndex].transform.position;
}
// Update is called once per frame
private void Update () {
if (moveAllowed && quizManager.allow) //here is the main part of code, if player is allowed to move "moveAllowed" and question is correct (quizManager.allow) piece can move.
Move();
}
//player move
private void Move()
{
if (waypointIndex <= waypoints.Length - 1)
{
transform.position = Vector2.MoveTowards(transform.position,
waypoints[waypointIndex].transform.position,
moveSpeed * Time.deltaTime);
if (transform.position == waypoints[waypointIndex].transform.position)
{
waypointIndex += 1;
}
}
}
}
Here is my quiz manager script where is methods for correct/wrong answers and question generation method
public class QuizManager : MonoBehaviour
{
public bool allow= false;
private static GameObject QuizCanvas;
public GameControl gameControl;
public List<QuestionAndAnswers> QnA;
public GameObject[] options;
public int currentQuestion;
public GameObject Quizpanel;
public GameObject GoPanel;
public Text QuestionTxt;
public Text ScoreTxt;
int totalQuestions = 0;
public int score;
private void Start()
{
totalQuestions = QnA.Count;
GoPanel.SetActive(false);
generateQuestion();
QuizCanvas = GameObject.Find("QuizCanvas");
}
public void retry()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
void GameOver()
{
Quizpanel.SetActive(false);
GoPanel.SetActive(true);
ScoreTxt.text = score + "/" + totalQuestions;
}
public void correct()
{
//when you are right
score += 1;
QnA.RemoveAt(currentQuestion);
galima = true;
StartCoroutine("closequiz");
StartCoroutine(waitForNext());
}
public void wrong()
{
//when you answer wrong
QnA.RemoveAt(currentQuestion);
allow = false;
StartCoroutine("closequiz");
StartCoroutine(waitForNext());
}
IEnumerator closequiz()
{
yield return new WaitForSeconds(1);
QuizCanvas.gameObject.SetActive(false);
}
IEnumerator waitForNext()
{
yield return new WaitForSeconds(1);
generateQuestion();
}
void SetAnswers()
{
for (int i = 0; i < options.Length; i++)
{
options[i].GetComponent<Image>().color = options[i].GetComponent<AnswerScript>().startColor;
options[i].GetComponent<AnswerScript>().isCorrect = false;
options[i].transform.GetChild(0).GetComponent<Text>().text = QnA[currentQuestion].Answers[i];
if(QnA[currentQuestion].CorrectAnswer == i+1)
{
options[i].GetComponent<AnswerScript>().isCorrect = true;
}
}
}
void generateQuestion()
{
if(QnA.Count > 0)
{
currentQuestion = Random.Range(0, QnA.Count);
QuestionTxt.text = QnA[currentQuestion].Question;
SetAnswers();
}
else
{
Debug.Log("Out of Questions");
GameOver();
}
}
}