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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Updating score for time intervals

Discussion in 'Scripting' started by Bazoozoo, Dec 1, 2015.

  1. Bazoozoo

    Bazoozoo

    Joined:
    Mar 27, 2015
    Posts:
    28
    I'm wanting to update the score every time i click the mouse button in a given time interval.
    Say for every second if you tap the screen either 0.2 seconds before or after that second the score will update.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class uiManager : MonoBehaviour {
    6.  
    7.     private int score;
    8.     private bool alreadyTapped;
    9.     private float tapTime;
    10.     public Text scoreText;
    11.  
    12.  
    13.  
    14.     // Use this for initialization
    15.     void Start ()
    16.     {
    17.         score = 0;
    18.         alreadyTapped = false;
    19.  
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update ()
    24.     {
    25.         if(Time.timeSinceLevelLoad > 0.8 && Time.timeSinceLevelLoad < 1.2)
    26.         {
    27.             if(Input.GetMouseButtonDown(0) && !alreadyTapped)
    28.             {
    29.                 score = score + 1;
    30.                 alreadyTapped = true;
    31.             }    
    32.         }
    33.         scoreText.text = ""+ score.ToString();
    34.     }    
    35. }
     
  2. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Okay so you want them to be able to click to update their points? You won't even need the bool.. Just make a float that has the amount of time you want to wait between clicks.. and have a timer that goes up according the the amount of time.. and it checks whether the time since the last click is over the tapTime.. Passing this the timer is reset and all the action happens. Simple :).

    Code (CSharp):
    1. private float tapTime = 1.2f;
    2.         private float timeElapsed = 0;
    3.         private int score = 0;
    4.  
    5.         void Update()
    6.         {
    7.             if(timeElapsed < tapTime)
    8.                 timeElapsed += Time.deltaTime;
    9.  
    10.             if (Input.GetMouseButtonDown(0) && timeElapsed >= tapTime)
    11.             {
    12.                 timeElapsed = 0;
    13.                 score++;
    14.                 scoreText.text = "" + score.ToString();
    15.             }
    16.         }
     
    Bazoozoo likes this.
  3. Bazoozoo

    Bazoozoo

    Joined:
    Mar 27, 2015
    Posts:
    28
    This allows the score to update even if it has missed the timeframe.

    How would I make it so you could only increase the score by one if you clicked between say 1 - 1.5seconds? And any other clicks outside this timeframe would result in decreased score?
     
  4. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    So you mean each time you click the reset counter starts and you have to click between 1 - 1.5secs afterwards?

    It's super simple really. It's only a matter of an if statement. If statements are used to check whether conditions are met. If so, you're able to use code. When you have more complicated actions that need more than one criteria to be met, you can have multiple conditions in one if statement. You can also have something called a nested if statement, which is basically when you have an if statement in an if statement, like so:

    Code (CSharp):
    1.         [SerializeField] // <-- This is a handy little thing. So you can make variables private, but so you can still see them in the inspector to change them manually.
    2.         private float minClickThreshold = 1.0f, maxClickThreshold = 1.5f; // <-- These are the min and max times you're allowed to click in.
    3.         private float timeElapsed = 0; // <-- The timer we that holds the amount of time since the last attempt.
    4.         private int score = 0; // I don't need to say, but my OCD wants me to leave a comment.....
    5.  
    6.         // This is called every game loop update. So very frequently lol.
    7.         void Update()
    8.         {
    9.             // This collect the time passing each frame. We're adding it to the counter each frame, so we have a little timer we can check against and reset when we need to, by setting to 0.
    10.             timeElapsed += Time.deltaTime;
    11.  
    12.             // So this is the first if statement. It's just checking to make sure you have the button clicked down, before checking anything else.
    13.             if (Input.GetMouseButtonDown(0))
    14.             {
    15.                 // Here, we've already checked before that the button is pressed down. So now we check whether the current time is before the max time and after the min.
    16.                 if(timeElapsed >= minClickThreshold && timeElapsed <= maxClickThreshold)
    17.                 {
    18.                     // Here the criteria was met, so we'll do the actions you want to happen when you click 'in time'
    19.                     score++;
    20.                     scoreText.text = "" + score.ToString();
    21.                 }
    22.  
    23.                 // This is inside the mouse click if statement.. So it'll reset it after all the checks have been done and they've tried clicking 'in time.
    24.                 // This needs to reset whether or not they won the point, so the timer can start counting for the next one. So we reset it outside of the nested if statement.
    25.                 timeElapsed = 0;
    26.             }
    27.         }
     
    Bazoozoo likes this.
  5. Bazoozoo

    Bazoozoo

    Joined:
    Mar 27, 2015
    Posts:
    28
    This is great, thanks so much for going to the effort to explain this so thoroughly. Can you recommend how i would go about setting up multiple timeframes? For example in a rhythm game where the beat isn't constant or you need to click at particular moments during the song and it isn't uniform.
     
  6. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    You're welcome :). Setting up multiple timeframes can be super duper easy or very complicated. It all depends on what it is exactly that you want to do. The super easy way is just to change the min and max values each frame. So something like this for example:

    Code (CSharp):
    1.         [SerializeField] // <-- This is a handy little thing. So you can make variables private, but so you can still see them in the inspector to change them manually.
    2.         private float minClickThreshold = 1.0f, maxClickThreshold = 1.5f; // <-- These are the min and max times you're allowed to click in.
    3.         private float timeElapsed = 0; // <-- The timer we that holds the amount of time since the last attempt.
    4.         private int score = 0; // I don't need to say, but my OCD wants me to leave a comment.....
    5.  
    6.         // This is called every game loop update. So very frequently lol.
    7.         void Update()
    8.         {
    9.             // This collect the time passing each frame. We're adding it to the counter each frame, so we have a little timer we can check against and reset when we need to, by setting to 0.
    10.             timeElapsed += Time.deltaTime;
    11.  
    12.             // So this is the first if statement. It's just checking to make sure you have the button clicked down, before checking anything else.
    13.             if (Input.GetMouseButtonDown(0))
    14.             {
    15.                 // Here, we've already checked before that the button is pressed down. So now we check whether the current time is before the max time and after the min.
    16.                 if (timeElapsed >= minClickThreshold && timeElapsed <= maxClickThreshold)
    17.                 {
    18.                     // Here each time you hit the threshold, the clicking 'in time' goalposts get ever so slightly smaller
    19.                     minClickThreshold += 0.01f; // <-- By doing += you can add whatever you want
    20.                     maxClickThreshold -= 0.01f; // <-- By doing i= you can minus whatever you want
    21.  
    22.                     // Here the criteria was met, so we'll do the actions you want to happen when you click 'in time'
    23.                     score++;
    24.                     scoreText.text = "" + score.ToString();
    25.                 }
    26.                 else // If the conditions ISN'T met, this'll happen..
    27.                 {
    28.                     // In this example, when you miss the 'in time' bit. Then counter is reset back to it's original size.
    29.                     minClickThreshold = 1.0f;
    30.                     maxClickThreshold = 1.5f;
    31.                 }
    32.  
    33.                 // This is inside the mouse click if statement.. So it'll reset it after all the checks have been done and they've tried clicking 'in time.
    34.                 // This needs to reset whether or not they won the point, so the timer can start counting for the next one. So we reset it outside of the nested if statement.
    35.                 timeElapsed = 0;
    36.             }
    37.         }
    If you wanted to orchestrate it a little more, you could make an array. An array is like any other variable, except it's not just one variable, but it's a list of variables of the same type. So a array of ints could be like 4, 7, 49, 3, type of thing. By putting a [SerializeField] above the array, you can make it as long as you want, with whatever min and max thresholds you'd like. Like this:

    Code (CSharp):
    1.         [SerializeField] // <-- This is a handy little thing. So you can make variables private, but so you can still see them in the inspector to change them manually.
    2.         private float[] minClickThreshold, maxClickThreshold; // If you click on the object holding this script in the inspector, you'll see that min and max thresholds
    3.                                                               // you can enter a number on. Choose a number and hit enter. You'll see a list pop up. Each time they click the next min and max thresholds will be checked against the reset timer in the list. I hope that makes sense.
    4.  
    5.         private float timeElapsed = 0; // <-- The timer we that holds the amount of time since the last attempt.
    6.         private int score = 0; // I don't need to say, but my OCD wants me to leave a comment.....
    7.         private int thresholdCount = 0;
    8.  
    9.         // This is called every game loop update. So very frequently lol.
    10.         void Update()
    11.         {
    12.             // This collect the time passing each frame. We're adding it to the counter each frame, so we have a little timer we can check against and reset when we need to, by setting to 0.
    13.             timeElapsed += Time.deltaTime;
    14.  
    15.             // So this is the first if statement. It's just checking to make sure you have the button clicked down, before checking anything else.
    16.             if (Input.GetMouseButtonDown(0))
    17.             {
    18.                 // Now we're doing something slightly different. Because the array holds a LIST of numbers, not just one.. In the [...] we need to specify which number we want.
    19.                 // These numbers are held in the order you entered them in the inspector, starting at the number 0. So, we start with the 0 in the list (array) of numbers,
    20.                 //by setting 'thresholdCount' to 0, then add 1 to it each time we try. So it'll check the next number in the list.
    21.                 if (timeElapsed >= minClickThreshold[thresholdCount] && timeElapsed <= maxClickThreshold[thresholdCount])
    22.                 {
    23.                     // Here the criteria was met, so we'll do the actions you want to happen when you click 'in time'
    24.                     score++;
    25.                     scoreText.text = "" + score.ToString();
    26.                 }
    27.  
    28.                 // This is inside the mouse click if statement.. So it'll reset it after all the checks have been done and they've tried clicking 'in time.
    29.                 // This needs to reset whether or not they won the point, so the timer can start counting for the next one. So we reset it outside of the nested if statement.
    30.                 timeElapsed = 0;
    31.  
    32.                 // We add one to the threshold each time they try to click the mouse in time with it. So we don't only reset the counter, but we ask for the next set of min/max thresholds.
    33.                 thresholdCount++;
    34.  
    35.             }
    36.         }
    Explaining anymore to you will have me making your entire game lol, so I won't do that.. but you can see all you need to do is change the min and max thresholds each time.. And you can do that however you want. Whether you want them to change over time, between each click attempt, anything. You just need to figure out your system. Good luck!
     
    Bazoozoo likes this.
  7. Bazoozoo

    Bazoozoo

    Joined:
    Mar 27, 2015
    Posts:
    28
    Thanks you've steered me in the right direction!
     
    Nigey likes this.