Calculating player distance meter? C# Unity2D


this is my first post here as a newbie to Unity. I just started learning c# few days ago and created my first game. Nevertheless, I encountered some minor issue of which is, calculating the player (flying and running sprites) distance. I managed to display the meter icon and “0” but it don’t know how to code it to calculate the player’s run.

here is what I did:

public Text meterLabel;

void meter{

meterLabel.text = meter.ToString();


Note: Im new to unity c# coding, i learnt that from some post i found in search for solution to my problem.


Any help would be appreciated.

Thanks in advance

There are several ways you can calculate the distance. Do know that unity units are meters. But that really doesn’t mater too much for most games.


If you know the speed at which your player is going at and it is a constant speed you can do speed x time. And that would give you the distance.

float startTime = Time.time;
float currentTime;

Void Update()
     currentTime = Time.time;
     float meters = (currentTime - startTime) * speed;
     meterLable.text = meters. ToString();

If you know the start point you can subtract the x values to get the distance.

Vector3 startPoint;

void Awake() 
    startPoint = transform.position;

void Update()
    float meters = transfom.position.x - startPoint.x;
    meterLable.text = meters.ToString();

If you need to know some more about programming in C# I suggest picking up a book at the library. Also there are some good unity tutorials out there. Learn more about unity’s API and what there is that you can use.