- Home /
 
check if an always changing int variable suddenly stop changing
i have this "distance" variable that is always changing(increasing) because I have an infinite runner. now if the player suddenly hits some obstacles which renders him unable to gain more distance which means the distance variable stop changing. how do i check that?
Answer by allenallenallen · Aug 07, 2015 at 04:35 AM
Instead of checking for an always changing integer, it would be a lot easier if you just check for obstacles and end the distance there.
But if you must, you can check if the distances are equal each frame for maybe 3 seconds before saying that it's the player's final score.
public int lastDistance = 0; // For comparison use. public int currentDistance = 0; // Player's current distance. public float timer = 0; public int seconds = 3; // Number seconds it checks for stabilizing the distance.
 void Update(){
     if (lastDistance == currentDistance){
         timer += Time.deltaTime;
         if (timer >= seconds) {
             // Done, records the currentDistance as the player's score.
         }
     }
     else {
         timer = 0;
     }
 
     lastDistance = currentDistance; 
 
 }
 
              Your answer