Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Scripting Help With Creating Timer Based Scoring?

Discussion in 'Scripting' started by ExcitedMormon, Mar 29, 2015.

  1. ExcitedMormon

    ExcitedMormon

    Joined:
    Aug 3, 2014
    Posts:
    7
    Hello Unity community! I would like to ask you guys for your generous help. I'm trying to create a timer based score system, but there are some things that I'm not sure what to do. The first is how the timer looks like, people on different places put float Timer = 0f; and then it goes up. However I do not like the look of the timer. I want the timer to look like this: 00:00:00 and go up by seconds.

    Now the second thing is how much points you get after the level is complete. I want the points to go higher when the time is lower, and go lower when the time is higher. So let's say you complete the level in 00:01:00, you would get 1000 points. If you complete it in 00:03:00, you would get 300 points or so.

    So how can I achieve this? These two things are the main problems that I'm having. I already now how to save with PlayerPrefs and get, but these things are preventing me from even beginning. Please help if you can.
     
  2. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    Take a look at here. You can find a basic explanation of how to turn your seconds into minutes, and you can imply the same logic for hours. Just write those values you get to a Text and show it on the screen. Now, for your score logic, you have to create a simple multiplier for that. If I were you, I would do the score calculations all based on the seconds passed, because seconds will be the smaller units in your case and it will make things simpler. You first need to have a total value, for example:

    Code (CSharp):
    1. private float f_Total;
    2.  
    3. if(minutes > 0)
    4. f_Total = (minutes * 60) + seconds;
    5. else
    6. f_Total = seconds;
    Now you know exactly how many seconds have passed, now is the part where you adjust your multiplier manually.

    Code (CSharp):
    1. public float f_Multiplier = 60; // in the units of seconds lets say.
    2. public float f_MaxScore; = 1000; // maximum score in one try, lets say 1000.
    3. private float f_Score;
    4.  
    5. f_Score = (f_MaxScore * f_Multiplier) / total;

    Now let's say user has completed it in 60 seconds. Total equals 60. f_Score variable will be (1000 * 60) / 60, which will result in 1000. So if user completes it in 60 seconds, his / her score will be 1000. Same logic goes for 3 minutes. 3 * 60 = 180, (1000 * 60) * 180 will result in 1000/3, something like 333.3~. Ofcourse you can limit your score, or turn it into an integer, but that's the logic. Cheers :)
     
    ExcitedMormon likes this.
  3. ExcitedMormon

    ExcitedMormon

    Joined:
    Aug 3, 2014
    Posts:
    7
    Thank you a lot for the quick reply! So far I've done the first step, so here's my code I was able to make:
    Code (CSharp):
    1. float timer;
    2.  
    3.     public bool TimeGo = true;
    4.  
    5.  
    6.     void Update()
    7.     {
    8.         if (TimeGo == true) {
    9.             timer++;
    10.         }
    11.     }
    12.  
    13.  
    14.     void OnGUI()
    15.     {
    16.  
    17.         int minutes = Mathf.FloorToInt(timer / 60F);
    18.         int seconds = Mathf.FloorToInt(timer - minutes * 60);
    19.        
    20.         string niceTime = string.Format("{0:00:00}:{1:00}", minutes, seconds);
    21.  
    22.         GUI.Label(new Rect(10,10,250 * 5,100 * 4), niceTime);
    23.        
    24.     }
    25. }
    26.  
    Pretty straight forward if I do say so myself. I haven't tried to do the second part yet so I will get back to you when I'm done or if I have any problems. Thank you for your time.:)