Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Domdom123 · Jan 01, 2021 at 06:22 PM · 2d gamecanvasmovement scriptboardgamedice

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();
         }
 
 
     }
 }





Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

197 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Playing a .mp4 video file after Scene change without Unity Pro? 0 Answers

What is best practices of 2d game for different screen sizes? 0 Answers

I am creating 2D top down shooter game but i am stuck at the movement. Help! 1 Answer

[2D] How to stop 2 vectors from adding force on each other 0 Answers

New Input System: 2D top-down RPG OnMouseDown(0) 'player move to mouse position' 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges