Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Distance and Best Distance

Discussion in 'Scripting' started by Louis-LCM, Sep 10, 2014.

  1. Louis-LCM

    Louis-LCM

    Joined:
    Aug 26, 2014
    Posts:
    59
    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.

    Code (JavaScript):
    1. //Score
    2. static var score : int = 0;
    3. static var distanceScore : int = 0;
    4. var distanceFont : Font;
    5. var style : GUIStyle;
    6. // Checking distance
    7. var previousPosition : Vector3;
    8. var calculatedDistance : float;
    9.     function OnStart()
    10.         {
    11.         }
    12.     function Awake()
    13.         {
    14.             previousPosition = transform.position;
    15.         }
    16.     function FixedUpdate () {
    17.         }
    18.     function Update()
    19.         {
    20.             calculatedDistance += (transform.position - previousPosition).magnitude;
    21.             previousPosition = transform.position;
    22.             distanceScore = Mathf.Round(calculatedDistance/0.1);  
    23.             score = distanceScore;
    24.         }
    25. function OnGUI()
    26. {
    27. GUI.Label(Rect(0, 50, 200, 30), "Distance: "+score, style);
    28. }
     
  2. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    var Travelling = false;
    var Score = 0;
    var BestScore = "";

    function Update(){
    if(Input.GetKeyDown(KeyCode.W)){
    transform.position(Vector3.forward * Time.deltaTime * 10);
    Travelling = true;
    }

    if(Travelling){
    Score += 1 * Time.deltaTime;
    }

    BestScore = "Best Score: " + Score;

    if(Application.Level(MainMenu)){
    Score = 0;
    }

    }

    function OnGUI(){
    GUI.Label(Rect(100,20,100,100),Score.ToString());
    GUI.Label(Rect(200,20,100,100),BestScore.ToString());
    }
     
  3. Louis-LCM

    Louis-LCM

    Joined:
    Aug 26, 2014
    Posts:
    59
    Thank you :D But I already solved that :D !
     
  4. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    No problem! Glad you fixed it :)
     
    Louis-LCM likes this.
  5. Louis-LCM

    Louis-LCM

    Joined:
    Aug 26, 2014
    Posts:
    59
    Thanks :D ! Now i'm trying to add a mute and unmute button and an settings menu :)) !
     
    Sykoo likes this.