Bien Ahora antes de establecer los turnos vamos a poner un oponente a nuestro «coche rojo»
vamos a nuestro Canvas y copiamos el botón de coche rojo, y nos saldrá un botón duplicado «Coche Rojo(1)» bien esto no es mas que un botón así que lo que vamos hacer es cambiar el nombre del nuevo botón por ejemplo botón amarillo o Amarillo como queramos
Vamos a nuestro botones ahora le pondré el color del Text amarillo, Ahora creamos un Script al que llamaremos Amarillo
El script es el mismo que el del coche Rojo de nuevo os lo pondré
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Amarillo: MonoBehaviour { public Text Resultado; private int total; public CARF Rott; int rpposicion; public int punto; bool movimie; // Start is called before the first frame update void Start() { total = 0; Resultado.text = ""; } // Update is called once per frame void Update() { if(Input.GetKeyDown(KeyCode.Q) && !movimie) { punto = Random.Range(1, 5); Debug.Log("Resul " + punto); total = punto; Resultado.text= " "+ total; if (rpposicion + punto <Rott.Puesto.Count){ StartCoroutine(Move()); } else { Debug.Log("Resultado"); } } } IEnumerator Move(){ if (movimie){ yield break; } movimie = true; while (punto > 0) { Vector3 nextPos = Rott.Puesto[rpposicion + 0].position; while (MoveToNexNode(nextPos)) { yield return null; } yield return new WaitForSeconds(0.1f); punto--; rpposicion++; } movimie = false; } bool MoveToNexNode(Vector3 goal){ return goal != (transform.position = Vector3.MoveTowards(transform.position, goal, 2f * Time.deltaTime)); } }
Lo único que cambia es el nombre del Script y hacemos lo mismo que hemos hecho con el coche de carreras rojo, una vez hecho esto ya tendríamos nuestro Oponente
aqui ya tendriamos nuestro Oponente el problema que no esta establecido el turno por lo que podriamos hacer facilmente trampa.
así que vamos a establecer turnos, lo primero que vamos hacer
Establecer Turnos
Bien vamos a crear un Create Empty
lo vamos a llamar «Controlador» y creamos un Script dentro de Controlador
bien yo ya lo tengo programado, básicamente pone el limite de turno y los turnos que se establecerán.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Control : MonoBehaviour { public int Turno = 0; public int LImitedeTurno = 2; public static Control control; void Start() { control = this.GetComponent<Control>(); } void Update() { if (Turno > 1) { Turno = 2; } } }
Aquí tenemos el Scrip ahora vamos al script de nuestros coches para establecer los turnos
vamos con el Script del coche rojo
aquí os dejare el Script entero pero primero os enseñare lo que incorporado para establecer los turnos.
en el script vamos a llamar al Script que hemos creado anteriormente
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class cocheRojo : MonoBehaviour { public Vector3 direccionMirada = new Vector3(0, -1f); public Text Resultado; private int total; public CARF Rott; int rpposicion; public int punto; bool movimie; public int Player1Turn = 1; // Start is called before the first frame update void Start() { total = 0; Resultado.text = ""; } // Update is called once per frame void Update() { if(Input.GetKeyDown(KeyCode.Q) && !movimie) { if (Player1Turn != Control.control.Turno) { return; } else if (Player1Turn == Control.control.Turno) { } punto = Random.Range(1, 5); Debug.Log("Resul " + punto); total = punto; Resultado.text= " "+ total; if (rpposicion + punto <Rott.Puesto.Count){ StartCoroutine(Move()); } else { Debug.Log("Resultado"); } if (Player1Turn.Equals(Control.control.Turno)) { if (Player1Turn > 2) { } Control.control.Turno = +2; } } } IEnumerator Move(){ if (movimie){ yield break; } movimie = true; if (Player1Turn.Equals(Control.control.Turno)) while (punto > 0) { Vector3 nextPos = Rott.Puesto[rpposicion + 0].position; while (MoveToNexNode(nextPos)) { yield return null; } punto--; rpposicion++; } movimie = false; } bool MoveToNexNode(Vector3 goal){ return goal != (transform.position = Vector3.MoveTowards(transform.position, goal, 2f * Time.deltaTime)); } }
bien aquí tendríamos el script del coche rojo ahora os enseñare el Script del coche amarillo que es practicamente igual
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class amarillo : MonoBehaviour { public Text Resultado; private int total; public CARF Rott; int rpposicion; public int punto; bool movimie; public int PlayerTurn = 2;////CODIGO DE INCORPORAR // Start is called before the first frame update void Start() { total = 0; Resultado.text = ""; } // Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.M) && !movimie) { if (PlayerTurn != Control.control.Turno)////CODIGO DE INCORPORAR { return;////CODIGO DE INCORPORAR } else if (PlayerTurn == Control.control.Turno)////CODIGO DE INCORPORAR { } punto = Random.Range(1, 5); Debug.Log("Resul " + punto); total = punto; Resultado.text = " " + total; if (rpposicion + punto < Rott.Puesto.Count) { StartCoroutine(Move()); } else { Debug.Log("Resultado"); } if (PlayerTurn > 1)////CODIGO DE INCORPORAR { } Control.control.Turno=+1;////CODIGO DE INCORPORAR } } IEnumerator Move() { if (movimie) { yield break; } movimie = true; if (PlayerTurn.Equals(Control.control.Turno))////CODIGO DE INCORPORAR while (punto > 0) { Vector3 nextPos = Rott.Puesto[rpposicion + 0].position; while (MoveToNexNode(nextPos)) { yield return null; } yield return new WaitForSeconds(0.1f); punto--; rpposicion++; } movimie = false; } bool MoveToNexNode(Vector3 goal) { return goal != (transform.position = Vector3.MoveTowards(transform.position, goal, 2f * Time.deltaTime)); } }
Y aquí lo tendríamos ahora lo guardamos y comprobamos
con esto los turnos estarían establecidos.