Race positions script.

Hey everyone,
I am here to ask for help on a script that I am trying to write.
It will calculate the positions of the player and the AI and put it on a text object to display it.
Although I don’t quite know how I will do this, I have the distance to the next waypoint, what lap the AI and player are on and what waypoint they had passed.
Any help on how to continue this would be amazingly appreciated.
As you can see so far I am getting all the information that I think I will need such as distance to the next waypoint but I just cannot think of how to carry it on. At first I started writing if statements but I figured it would take tens of them so there must be an easier way.

using UnityEngine;
using System.Collections;

public class RacePositions : MonoBehaviour {

    private int PlayerOnePosition;
    private int AiOnePosition;
    private int AiTwoPosition;
    private int AiThreePosition;

    private float PlayerOneDistance;
    private float AiOneDistance;
    private float AiTwoDistance;
    private float AiThreeDistance;

    private int PlayerOneWaypoints;
    private int AiOneWaypoints;
    private int AiTwoWaypoints;
    private int AiThreeWaypoints;

    private int PlayerOneLaps;
    private int AiOneLaps;
    private int AiTwoLaps;
    private int AiThreeLaps;

    private GameObject PlayerOneTarget;
    private Transform AiOneTarget;
    private Transform AiTwoTarget;
    private Transform AiThreeTarget;

    // Use this for initialization
    void Start () {
  
    }
  
    // Update is called once per frame
    void Update () {

        Debug.Log ("P1: " + PlayerOneDistance + "AI 1: " + AiOneDistance + "AI 2: " + AiTwoDistance + "AI 3: " + AiThreeDistance);

        PlayerOneTarget = GameObject.FindWithTag ("Waypoint " + GameObject.Find ("Player One").GetComponent<LapCounter3D> ().WaypointsPassed);
        AiOneTarget = GameObject.Find ("AI 1").GetComponent<UnityStandardAssets.Vehicles.Car.CarAIControl> ().m_Target;
        AiTwoTarget = GameObject.Find ("AI 2").GetComponent<UnityStandardAssets.Vehicles.Car.CarAIControl> ().m_Target;
        AiThreeTarget = GameObject.Find ("AI 3").GetComponent<UnityStandardAssets.Vehicles.Car.CarAIControl> ().m_Target;

        PlayerOneWaypoints = GameObject.Find ("Player One").GetComponent<LapCounter3D> ().WaypointsPassed;
        AiOneWaypoints = GameObject.Find ("AI 1").GetComponent<LapCounter3D> ().WaypointsPassed;
        AiTwoWaypoints = GameObject.Find ("AI 2").GetComponent<LapCounter3D> ().WaypointsPassed;
        AiThreeWaypoints = GameObject.Find ("AI 3").GetComponent<LapCounter3D> ().WaypointsPassed;

        PlayerOneLaps = GameObject.Find ("Player One").GetComponent<LapCounter3D> ().LapCount;
        AiOneLaps = GameObject.Find ("AI 1").GetComponent<LapCounter3D> ().LapCount;
        AiTwoLaps = GameObject.Find ("AI 2").GetComponent<LapCounter3D> ().LapCount;
        AiThreeLaps = GameObject.Find ("AI 3").GetComponent<LapCounter3D> ().LapCount;

        //Calculate the distance between me and the target which
        //would be the next waypoint.
        PlayerOneDistance = Vector3.Distance (GameObject.Find("Player One").transform.position, PlayerOneTarget.transform.position);
        AiOneDistance = Vector3.Distance (GameObject.Find ("AI 1").transform.position, AiOneTarget.transform.position);
        AiTwoDistance = Vector3.Distance (GameObject.Find ("AI 2").transform.position, AiTwoTarget.transform.position);
        AiThreeDistance = Vector3.Distance (GameObject.Find ("AI 3").transform.position, AiThreeTarget.transform.position);
    }
}

Thanks.
EDIT:
This goes on my _RaceManager gameobject.

Aside from your problem, your code is highly inefficient. Create references to the objects instead of trying to find them. Use a list to order them and fill the list at startup. Then check if the references are null before you do anything with them. Your current code is bound to give you problems in the future.

1 Like

xD Ok, I was going to save that for later as I mocked this up in about 5 minutes but I will get on it.
EDIT: This works for now but I will definitely make it better for the future.

the highest lap with the greatest distance travelled(the highest ranking waypoint passed) in that lap wins?
for example player.currentLap = 3; player.lastPassedWaypoint = 6;
wins from
AI.currentLap = 3; AI.lastPassedWaypoint = 5;

Yeah when the player or AI passes across the start line again the waypointpassed returns to 1 (from 10) and +1 lap is given.

so then, maybe i missunderstood your question… isn’t that what you want?

I would like to display the race positions on screen. I have this information but I dont know how to carry it on so that it updates in real time.

Put everything into a list and then sort it.

Collections are more then just convenience. There are real benefits to using the right collection.

Thanks, I will check it out.

Ah ok, just make a UI Text, get it’s text variable in your race script and assign the name of the nr1 orso…

something like this:

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

public class DisplayPosition : MonoBehaviour
{

    public Text display;

    void Update ()
    {
        display.text = RaceManager.CurrentLeader;
    }
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class RaceManager : MonoBehaviour
{
    public static List<Contestant> contestants = new List<Contestant>();

    public static string CurrentLeader
    {
        get
        {
            //calculate who's first
            contestants.OrderBy(o=>o.Position).ToList();
            return contestants[0].name;
        }
    }
}
[System.Serializable]
public class Contestant
{
    public string name;
    public int currentLap;
    public int lastPassedWayPoint;

    public Contestant(){}

    public float Position
    {
        get
        {
            return currentLap+(lastPassedWayPoint/10);
        }
    }
}

Thanks, this will help a lot.