Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by viketga · Nov 30, 2021 at 10:12 AM · c++

How to make player roll dice once more and stop the player turn once?

So im making snake and ladder game but i want to add some customize in it. If player1 stop at 32, player1 wont get the turn once or twice. And if player1 stop at 40,player1 can roll the dice once more. Please help

This is my code for game control

 using UnityEngine;
 using UnityEngine.UI;
 
 public class GameControl : MonoBehaviour {
 
     private static GameObject whoWinsTextShadow, player1MoveText, player2MoveText;
 
     private static GameObject player1, player2;
 
     public static int diceSideThrown = 0;
     public static int player1StartWaypoint = 0;
     public static int player2StartWaypoint = 0;
 
     public static bool gameOver = false;
 
     // Use this for initialization
     void Start () {
 
         whoWinsTextShadow = GameObject.Find("WhoWinsText");
         player1MoveText = GameObject.Find("Player1MoveText");
         player2MoveText = GameObject.Find("Player2MoveText");
 
         player1 = GameObject.Find("Player1");
         player2 = GameObject.Find("Player2");
 
         player1.GetComponent<FollowThePath>().moveAllowed = false;
         player2.GetComponent<FollowThePath>().moveAllowed = false;
 
         whoWinsTextShadow.gameObject.SetActive(false);
         player1MoveText.gameObject.SetActive(true);
         player2MoveText.gameObject.SetActive(false);
     }
 
     // Update is called once per frame
     void Update()
     {
         if (player1.GetComponent<FollowThePath>().waypointIndex > 
             player1StartWaypoint + diceSideThrown)
           
         {
             //Debug.Log(player1StartWaypoint+diceSideThrown);
             if(player1StartWaypoint+diceSideThrown == 4){
                 player1.GetComponent<FollowThePath>().transform.position = player1.GetComponent<FollowThePath>().waypoints[37].transform.position;
                 player1.GetComponent<FollowThePath>().waypointIndex = 37;
                 player1.GetComponent<FollowThePath>().waypointIndex +=1;
                 MovePlayer(1);
             }
              if(player1StartWaypoint+diceSideThrown == 32){
                 player2.GetComponent<FollowThePath>().moveAllowed = false;
              
             }
             if(player1StartWaypoint+diceSideThrown == 50){
                 player1.GetComponent<FollowThePath>().transform.position = player1.GetComponent<FollowThePath>().waypoints[90].transform.position;
                 player1.GetComponent<FollowThePath>().waypointIndex = 90;
                 player1.GetComponent<FollowThePath>().waypointIndex +=1;
                 MovePlayer(1);
             }
             if(player1StartWaypoint+diceSideThrown == 74){
                 player1.GetComponent<FollowThePath>().transform.position = player1.GetComponent<FollowThePath>().waypoints[92].transform.position;
                 player1.GetComponent<FollowThePath>().waypointIndex = 92;
                 player1.GetComponent<FollowThePath>().waypointIndex +=1;
                 MovePlayer(1);
             }
             if(player1StartWaypoint+diceSideThrown == 71){
                 player1.GetComponent<FollowThePath>().transform.position = player1.GetComponent<FollowThePath>().waypoints[54].transform.position;
                 player1.GetComponent<FollowThePath>().waypointIndex = 54;
                 player1.GetComponent<FollowThePath>().waypointIndex +=1;
                 MovePlayer(1);
             }
             if(player1StartWaypoint+diceSideThrown == 33){
                 player1.GetComponent<FollowThePath>().transform.position = player1.GetComponent<FollowThePath>().waypoints[24].transform.position;
                 player1.GetComponent<FollowThePath>().waypointIndex = 24;
                 player1.GetComponent<FollowThePath>().waypointIndex +=1;
                 MovePlayer(1);
             }
             if(player1StartWaypoint+diceSideThrown == 30){
                 player1.GetComponent<FollowThePath>().transform.position = player1.GetComponent<FollowThePath>().waypoints[11].transform.position;
                 player1.GetComponent<FollowThePath>().waypointIndex = 11;
                 player1.GetComponent<FollowThePath>().waypointIndex +=1;
                 MovePlayer(1);
             }
             
         
             player1.GetComponent<FollowThePath>().moveAllowed = false;
             player1MoveText.gameObject.SetActive(false);
             player2MoveText.gameObject.SetActive(true);
             player1StartWaypoint = player1.GetComponent<FollowThePath>().waypointIndex - 1;
         }
 
         if (player2.GetComponent<FollowThePath>().waypointIndex >
             player2StartWaypoint + diceSideThrown)
         {
          
             //Debug.Log(player1StartWaypoint+diceSideThrown);
             if(player2StartWaypoint+diceSideThrown == 4){
                 player2.GetComponent<FollowThePath>().transform.position = player2.GetComponent<FollowThePath>().waypoints[37].transform.position;
                 player2.GetComponent<FollowThePath>().waypointIndex = 37;
                 player2.GetComponent<FollowThePath>().waypointIndex +=1;
                 MovePlayer(2);
             }
             if(player2StartWaypoint+diceSideThrown == 50){
                 player2.GetComponent<FollowThePath>().transform.position = player2.GetComponent<FollowThePath>().waypoints[90].transform.position;
                 player2.GetComponent<FollowThePath>().waypointIndex = 90;
                 player2.GetComponent<FollowThePath>().waypointIndex +=1;
                 MovePlayer(2);
             }
             if(player2StartWaypoint+diceSideThrown == 74){
                 player2.GetComponent<FollowThePath>().transform.position = player2.GetComponent<FollowThePath>().waypoints[92].transform.position;
                 player2.GetComponent<FollowThePath>().waypointIndex = 92;
                 player2.GetComponent<FollowThePath>().waypointIndex +=1;
                 MovePlayer(2);
             }
             if(player2StartWaypoint+diceSideThrown == 71){
                 player2.GetComponent<FollowThePath>().transform.position = player2.GetComponent<FollowThePath>().waypoints[54].transform.position;
                 player2.GetComponent<FollowThePath>().waypointIndex = 54;
                 player2.GetComponent<FollowThePath>().waypointIndex +=1;
                 MovePlayer(2);
             }
             if(player2StartWaypoint+diceSideThrown == 33){
                 player2.GetComponent<FollowThePath>().transform.position = player2.GetComponent<FollowThePath>().waypoints[24].transform.position;
                 player2.GetComponent<FollowThePath>().waypointIndex = 24;
                 player2.GetComponent<FollowThePath>().waypointIndex +=1;
                 MovePlayer(2);
             }
             if(player2StartWaypoint+diceSideThrown == 30){
                 player2.GetComponent<FollowThePath>().transform.position = player2.GetComponent<FollowThePath>().waypoints[11].transform.position;
                 player2.GetComponent<FollowThePath>().waypointIndex = 11;
                 player2.GetComponent<FollowThePath>().waypointIndex +=1;
                 MovePlayer(2);
             }
             player2.GetComponent<FollowThePath>().moveAllowed = false;
             player2MoveText.gameObject.SetActive(false);
             player1MoveText.gameObject.SetActive(true);
             player2StartWaypoint = player2.GetComponent<FollowThePath>().waypointIndex - 1;
         }
 
         if (player1.GetComponent<FollowThePath>().waypointIndex == 
             player1.GetComponent<FollowThePath>().waypoints.Length)
         {
             whoWinsTextShadow.gameObject.SetActive(true);
             whoWinsTextShadow.GetComponent<Text>().text = "Player 1 Wins";
             gameOver = true;
         }
 
         if (player2.GetComponent<FollowThePath>().waypointIndex ==
             player2.GetComponent<FollowThePath>().waypoints.Length)
         {
             whoWinsTextShadow.gameObject.SetActive(true);
             player1MoveText.gameObject.SetActive(false);
             player2MoveText.gameObject.SetActive(false);
             whoWinsTextShadow.GetComponent<Text>().text = "Player 2 Wins";
             gameOver = true;
         }
     }
 
     public static void MovePlayer(int playerToMove)
     {
         switch (playerToMove) { 
             case 1:
                 player1.GetComponent<FollowThePath>().moveAllowed = true;
                 break;
 
             case 2:
                 player2.GetComponent<FollowThePath>().moveAllowed = true;
                 break;
         }
     }
 }


This is the dice code

 using System.Collections;
 using UnityEngine;
 
 public class Dice : MonoBehaviour {
 
     private Sprite[] diceSides;
     private SpriteRenderer rend;
     private int whosTurn = 1;
     private bool coroutineAllowed = true;
 
     // Use this for initialization
     private void Start () {
         rend = GetComponent<SpriteRenderer>();
         diceSides = Resources.LoadAll<Sprite>("DiceSides/");
         rend.sprite = diceSides[5];
     }
 
     private void OnMouseDown()
     {
         if (!GameControl.gameOver && coroutineAllowed)
             StartCoroutine("RollTheDice");
     }
 
     private 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(1);
         } else if (whosTurn == -1)
         {
             GameControl.MovePlayer(2);
         }
         whosTurn *= -1;
         coroutineAllowed = true;
     }
 }


This is followthepath code

 using UnityEngine;
 
 public class FollowThePath : MonoBehaviour {
 
     public Transform[] waypoints;
 
     [SerializeField]
     private float moveSpeed = 1f;
 
     [HideInInspector]
     public int waypointIndex = 0;
 
     public bool moveAllowed = false;
 
     // Use this for initialization
     private void Start () {
         transform.position = waypoints[waypointIndex].transform.position;
     }
     
     // Update is called once per frame
     private void Update () {
         if (moveAllowed)
             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;
             }
         }
     }
 }
 

 





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

131 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

Related Questions

Developing for IOS 1 Answer

Perlin Noise. 1 Answer

Access and pass variables between multiple scripts 2 Answers

How to step into a native C++ dll in Visual Studio? 0 Answers

Can't open device camera with Native plugins using OpenCV and c++. 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