2D Racer Game

010: Race Position

create a script called "positions" add this script to the lap counter and attach the UI.text elements in the inspector

add the player and set the total laps.

 

JS

CURRENTLY UNAVAILABLE

C#

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

public class positions : MonoBehaviour {


    // Use this for initialization


    public List<GameObject> cars = new List<GameObject>();
    public Text lapDisplay;
    public Text posDisplay;

    public int lapNumber;
    public int raceLaps;
    public GameObject PLAYER;
    public int crntPosition;
    public static int finalPosition;

    void Start () {

        lapNumber = 0;
        Debug.Log (cars.Count);
        
    }
    
    // Update is called once per frame
    void LateUpdate () {

        lapDisplay.text = "LAP: "+ PLAYER.GetComponent<progressRace>().lap + ":" + raceLaps;
        posDisplay.text = "POS: " + crntPosition;

        if (PLAYER.GetComponent<progressRace> ().lap == raceLaps + 1) {

            finalPosition = crntPosition;
            Application.LoadLevel (2);

        }
        if (PLAYER.GetComponent<progressRace> ().lap > 0) {
            StartCoroutine (writePositions ());
        }



    }

    IEnumerator writePositions(){

        cars.Sort(SortByScore);
        yield return new WaitForSeconds(0.1f);
    

        for(int i=0; i < 13; i++){
            if(cars[i].name == "PLAYER") {
                crntPosition = i+1;
                PLAYER.GetComponent<progressRace> ().position = crntPosition;
            }
        }



    }

            static int SortByScore(GameObject p1, GameObject p2)
            {
                return p2.GetComponent<progressRace>().totalComp.CompareTo(p1.GetComponent<progressRace>().totalComp);
            }
}

 

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.