2D Racer Game

09: Lap Counter & Progress

Create an empty game object and add a 2D box collider. Tag it as "LAP".

Screen Shot 2017 11 23 at 15.52.05

Create a new script called "progressRace" and attach it to the player car and AI car.

JS

CURRENTLY UNAVAILABLE

C#

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

public class progressRace : MonoBehaviour {

    public Transform[] checkpoints;
    public GameObject lapCounter;
    public int lap;
    public int crntCheckpoint;
    public float dist;
    public float totalComp;
    public int position;
    // Use this for initialization
    void Start () {

        lap = 0;
        crntCheckpoint = 0;
        
    }
    
    // Update is called once per frame
    void LateUpdate () {

        dist = Vector3.Distance (transform.position, checkpoints[crntCheckpoint].position);
        int laps = lap * 1000;
        int checks = crntCheckpoint * 100;
        totalComp = laps + checks + dist;

    }

    void OnTriggerEnter2D(Collider2D enter2D)
    {

        if (enter2D.gameObject.tag == "LAP") {
            lap += 1;
            }
    

    if(enter2D.gameObject.tag == "CHECKPOINT") {

        if(crntCheckpoint < checkpoints.Length-1 && lap > 0){
            crntCheckpoint += 1;
        }
        else{crntCheckpoint = 0;}



    }
}
}

 Now create a series of checkpoints just like the lap counter and tag them as "CHECKPOINT". Add the checkpoints to the checkpoints array.

Casino Bet 365 is best casino in the world.

What's Next

Next up for Unity Snippets is a look at game development articles from across the web...

Free Templates - bigtheme.net
Popular Art Betting make bookies articles.