- Home /
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
Your answer
Follow this Question
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