- Home /
Check when closest to 0
The point of my game is to be as close to 0 as possible. How can I set highscore based on how close you are to 0. Let's say the last highscore is 0.12, then I get 0.09, then that should be the new high score. Then let's say I get -0.02, than that's the new highscore. I know how to use get and set int/float, so that's not what i'm asking. I'd really appreciate answers!
Hey! I'm not sure if this would work but you could do a check each time the score is created and if it is a highscore overwrite a variable. For example:
var score;
var highscore;
if(score < 0){
if(highscore < 0){
if((score * -1) < (highscore * -1)){
highscore = score;
}
}
else{
if((score * -1) < highscore){
highscore = score;
}
}
}
else{
if(highscore < 0){
if(score < (highscore * -1)){
highscore = score;
}
}
else{
if(score < highscore){
highscore = score;
}
}
}
I haven't tested this but I think it might work, sorry if this isn't what your looking for :) Hopefully what this does is check if the score is negative and multiplies by -1 if it is(this will give us a positive value to compare to). Then the same is done for the current highscore incase that is negative as well e.g -0.02 . Then the closest to 0 value should be saved into highscore. P.S This is not refactored, you can probably write the same code in a couple of lines but i'm a noob :)
Answer by $$anonymous$$ · Aug 17, 2015 at 07:32 PM
There is not enough information here to really be able to help you a whole lot. Do you want something like:
if (Mathf.Abs(newScore) < Mathf.Abs(highscore)) highscore = newScore;
This checks the absolute value of the new score and compares it's total distance from zero to the current highscore's.
Answer by Maui-M · Aug 17, 2015 at 07:37 PM
You can use absolute value to normalize the values then take the lowest one.
if(Mathf.Abs(score) < Mathf.Abs(highscore))
highscore = score;