- Home /
Distance and Best Distance
Hi ! I'm new to unity. First sorry for my bad english.
I want to make a distance and a best distance travelled system . I want the distance to be displayed in the game scene and the best distance in main menu scene ... Here is what i have so far.
//Score
static var score : int = 0;
static var distanceScore : int = 0;
var distanceFont : Font;
var style : GUIStyle;
// Checking distance
var previousPosition : Vector3;
var calculatedDistance : float;
function OnStart()
{
}
function Awake()
{
previousPosition = transform.position;
}
function FixedUpdate () {
}
function Update()
{
calculatedDistance += (transform.position - previousPosition).magnitude;
previousPosition = transform.position;
distanceScore = Mathf.Round(calculatedDistance/0.1);
score = distanceScore;
}
function OnGUI()
{
GUI.Label(Rect(0, 50, 200, 30), "Distance: "+score, style);
}
Answer by Stormizin · Sep 10, 2014 at 07:27 PM
You can use Player Prefs to store a variable with the best distance and then get this variable and show at any part of your game.
Hope it helps.
Thanks ... I know that ... but they don't work ... Idk why ...I really can't figure how I can do it ... I also used PlayerPrefs at coins system ... but here they don't work ...
Ins$$anonymous$$d of Player Prefs I often just use a Game Controller script for variables which I keep around the whole game with a DontDestroyOnLoad added to it.