- Home /
How to Check if my Score is Divisible by 10 for More than 1 Frame?
Hello, everyone. I am making a 2D game where as the player earns points, the camera zooms out. Every time the score is divisible by 10, the camera zooms out (increases in size) by 0.1. It works as it should, except that for every frame the score is divisible by 10, the camera keeps zooming out. How would I tell the game not to zoom out again if the score was also divisible by 10 on the previous frame? Or is there another solution to this?
void Update () {
if (score % 10 == 0 && score != 0)
{
Camera.main.orthographicSize += 0.1f;
}
}
Thank you for your help!
Answer by Trevdevs · Aug 17, 2018 at 05:28 PM
Its zooming out everytime because its in the update function.
what your code says here is if during this frame the score is divisible by 10 and score is not 0 zoom out... repeat every frame until the score is not divisible by 10.
I would put your script in a function where you add your score so that when the score is added it checks if its divisible by 10 and increases the size and doesn't repeat until the score is added again.
Sorry... I'm rather new to coding... how would I tell the function not to run until the score is added again?
you have a function where you are adding the score right? or how are you adding the score? where you have score += 1 put the if statement there
Answer by jeremydulong · Aug 17, 2018 at 06:27 PM
Update is run every frame in Unity. I would use Update to check if the score is higher than it was the last time it was divisible by 10 and then do it that way.
pseudo code:
// SET THESE TO THE SAME VALUE INITIALLY
float score = 0
float scoreCheck = 0
Update() {
checkScore();
}
void checkScore() {
if (score % 10 == 0 && score > scoreCheck) {
// do your camera stuff
scoreCheck = score;
}
}
Your answer
Follow this Question
Related Questions
How do you stop the camera redrawing the frame when nothing has moved? 0 Answers
vBlank Drift - Locking unity to the vBlank? 1 Answer
is there a way i can make a game object destroy itself pixel by pixel frame by frame 1 Answer
Multiply float by int? 2 Answers
How do you find Unity's frames/second? 4 Answers