Aquí lo que vamos a hacer es algo muy simple. Aquí os voy a dejar el script del coche.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class cocheRojo : 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)); } }
Aquí tenemos el primer script del coche rojo lo único que tendríamos que añadir:
public void movercocherojoboton(){
}
ahora solo nos quedaría copiar lo que está en «Update» excepto «if(Input.GetKeyDown(KeyCode.Q) && !movimie)», ya que si esto lo dejamos no, nos funcionaria
debería de quedar así
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class cocheRojo : 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)); } } public void movercocherojoboton){ 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"); } }
Ahora solo faltaría adjuntárselo al botón, para que al pulsar el coche se mueva. así que deberiamos de hacer lo siguiente.
y ya lo tendriamos.