Mathf.Round

Hello,
So ive got a small problem.
Im rounding the x position(local) from an Gameobject im Instantiating per script in 10 steps. But then if the position is 20 or 40 or 60etc. it should do + 10.
So this isnt the problem because i know who to di that. But my solution is very complicated and long:

int a = Mathf.RoundToInt((int)RandomStartPointX / 10f) * 10;
       
        if(a == 0 || a == 20 || a == 40 || a == 60 ||a == 80 || a == 100 || a == 120 || a == 140 || a == 160 || a ==  180 || a ==  200
        || a == 220 || a == 240 || a == 240 || a == 260 || a == 280 || a == 300 || a == 320 || a == 340 || a == 360 || a == 380 || a == 400 || a == 420
        || a == 440 || a == 460 || a == 480 || a == 500 || a == 520 || a == 540 || a == 560 || a == 580 || a == 600 || a == 620 || a == 640 || a == 680
        || a == 700 || a == 720 || a == 740 || a == 760 || a == 780 || a == 800 || a == 820 || a == 840 || a == 860 || a == 880 || a == 900 || a == 920
        || a == 940 || a == 960 || a == 980 || a == 1000)
        a += 10;

I get excatly the output i want to.
But i need to do these if statements till a == 10000 and dont want to write half my script if statements.
Is there any way to bypass the if statements and still get the same solution?

You probably want to use the remainder operator. As in use the operator to check if there is zero remainder after dividing by 20, then perform the necessary action.

2 Likes

A For loop where for any value of a, a = Mathf.RoundToInt(a + (10.0f-a))?

Not tested. :slight_smile:

yep thats what i was searching for. Thanks