Contador de vueltas

Lo siguiente que vamos aprender es a contar las vueltas de nuestro coche,  es una forma bastante sencilla de hacer

Aquí tengo un circuito el primero es un objetoVacio ChequePoin, el segundo es la línea de Meta «StartFine»  donde el jugador sumara las vueltas, cuando pase por chechkpoinnt1 y chechkpoinnt2

Pero aquí os lo voy a poner desde el principio tendremos un contador de vueltas, y  también un contador de tiempo que contara el tiempo que hacemos en cada vuelta, así que empezaremos de 0.

en nuestro juego vamos a crear un Canvas

Después yo le voy a crear un Panel pero esto es solo Opcional

Ahora vamos a crear los textos donde nos diga el tiempo y las vueltas

Ahora crearemos dos mas   y pondremos 0

Ahora vamos a crear los objetos vacíos

Al primer objeto vacío le llamare ChequePoint, tal cual le tengo yo en la imagen

Ahora creare dos objetos vacíos mas dentro de ChequePoint, StartFine, checkpoint1, checkpoint1, a estos 3 objetos le voy añadir un Box Collider2D, y les activare el Is Trigger

Ahora vamos a crear un Script, a nuestro vehículo

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Sitemadevueltas : MonoBehaviour
{
    public float timer = 0;
    private bool startTimer = false;
    public float Vuelta = 0;

    private bool checjpoint1 = false;
    private bool chechkpoinnt2 = false;

    public Text Tiempo;
    public Text Vueltas;
    void Start()
    {
        timer = 0;
        Vuelta = 0;
    }

    void Update()
    {

        if (startTimer == true)
        {
            timer = timer + Time.deltaTime;

            Tiempo.text = "  " + timer;
            Vueltas.text = "  " + Vuelta;
        }
    }


    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.name == "Startfine")
        {

            if (checjpoint1 == true && chechkpoinnt2 == true)
            {
                startTimer = false;

                if (Vuelta == 0)
                {
                    Vuelta = timer;
                }


                if (timer < Vuelta)
                {
                    Vuelta = timer;

                }
                startTimer = true;
                timer = 0;
                Vuelta += 1;
                checjpoint1 = false;
                chechkpoinnt2 = false;



            }
        }

        if (other.gameObject.name == "chechkpoinnt1")
        {
            Debug.Log("chechkpoinnt1");
            checjpoint1 = true;
        }

        if (other.gameObject.name == "chechkpoinnt2")
        {
            Debug.Log("chechkpoinnt2");
            chechkpoinnt2 = true;
        }
    }
}

Este seria nuestro código

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *