hello, I have this variable that keeps on increasing as time passes. along the way it will stop increasing. so how do i detect that so i could make an if statement when it happens.
How often does this variable increase? If you do know the time interval it increases though, that would be the best.
public int number = 0;
public int lastNumber = -1;
if (number == lastNumber){
// number has stopped increasing.
}
else {
lastNumber = number;
}
Store its value in a “previousValue” variable and then at each cycle (update) check if value <= previousValue
. If “yes” it means it has stopped increasing.
Possibly you can handle it using a flag variable. The condition opposite to which increases the variable can be used. This way you don’t have to compare everytime if the current value is similar to the last one. If the flag variable is set then you are good to go.
For example:
bool isRunning;
float distance;
void Update()
{
if(Input.GetAxis("Horizontal"))
{
distance += 10.0f;
isRunning = true;
}
else
{
isRunning = false;
// Variable stop increasing
}
}
void SomeFunction()
{
if(!isRunning)
{
// You can go with this
}
}
So, if I got this right, you’re using your variable “distance” as your global score, essentially. When -something- happens (pausing the game could serve as an example), the “distance” variable no longer increases, but the background continues scrolling by.
Why not simply link the background scrolling to your “distance” variable? You could use something along the lines of:
float screenWidth = 30.0f; // Distance to scroll before repeating or replacing texture
// ...
background.position = new Vector2(Mathf.Repeat(distance, screenWidth), 0.0f);
Using something along these lines, your background’s position directly correlates with the distance traveled, so if your score stops rising, the background stops moving.
Then, you can have another variable influence the speed further with a multiplier, so rather than just using “distance”, it’s “distance * scale” instead (with a few tweaks, but still…). That way, the scale can make the scrolling speed up or slow down to fit the mood of the game at the time, or to focus on background elements, but whether it actively moves or not would still be dependent on whether the distance variable is changing.
An alternative to using “distance” directly would be to instead make relative movements based on the amount “distance” changes per frame.
float distanceDelta;
// ...
distanceDelta = distance - lastFrameDistance;