- Home /
Calculating player distance meter? C# Unity2D
Hello,
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.
Apologies.
Any help would be appreciated.
Thanks in advance
@$$anonymous$$akecodenow,
how do you mean? Please can you give me a brief insight? Im totally new at this.
Thank you for the scripts. $$anonymous$$y actual game idea is similar to jetpack joyride. How do I implement it?
Thanks again ;)
Answer by immersiveGamer · Jan 26, 2015 at 06:27 AM
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.
Speed
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();
}
Distance 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.
Do know that unity units are meters
They are Units. Unspecified, is the relation to an actual measurement.
eh, yes for most basic games it doesn't matter but it really is meters because when you import models and do the units to meters it will be 1 meter to 1 unit and physics is based off of meters and kilograms http://answers.unity3d.com/questions/517442/what-are-the-real-world-measurements-in-unity.html
Hey!
I used the example of the speed, but it appears to float, I wanted to appear in int, to do this?
just parse the value: int number = (int)value;
Your answer
Follow this Question
Related Questions
Keep score after restart & limit ads/hour 1 Answer
Points - Kills fps 1 Answer