- Home /
detect if a variable has stop increasing
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.
um sorry for not being clear guys. my variable is 'distance' and it increases overtime since i have a 2d ifinite runner. it stops sometimes for reasons not important. the reason why i want to detect this is because i want to stop the background animation. cause it looks weird when you the player stop getting more distance, but the background scrolling animation is still going on. I can stop the animation no problem, the problem is just the detection so I can stop it.
@NeverHopeless im sorry but "if(Input.GetAxis("Horizontal"))" is confusing me is it being use everytime? or when Input is on horizontal only. i kinda need it to would work everytime.
@Rhylvin2015, if there is an input for this axis it will go inside if statement. I just give an example, not sure how did you setup things in your game for taking input from user.
Answer by allenallenallen · Aug 11, 2015 at 08:00 AM
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;
}
Answer by GiyomuGames · Aug 11, 2015 at 08:02 AM
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.
Answer by NeverHopeless · Aug 11, 2015 at 08:31 AM
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
}
}
Answer by Eno-Khaon · Aug 12, 2015 at 12:09 AM
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;
Just before you calculate your distance variable, you can define the value of what it was before the current frame.
// First, set your previous distance to what it was last.
lastFrameDistance = distance;
// Then, calculate your new distance value.
distance = (However you're deciding it);
The difference between the two is how far you traveled in one frame.
Your answer
Follow this Question
Related Questions
controlling the increment in the variable 1 Answer
Pass a function(float) as variable 2 Answers
Displaying variable on UI text every frame (JS) 1 Answer
How do Generate Random Unique int ? 1 Answer