How to "round numbers in increments"

Hi! I want to allow players in my game to face parts towards them, but I was also thinking about having it done in increments of 15 degrees.

Can someone with more LUA math skill please help explain to me how this could be achieved? I know that I can use math.floor() to get to the nearest whole number, but for this purpose rounding to a degree isn’t worth it.

For example, the math operation would take in 57 degrees and return 60 degrees, 128 returns 120, etc.

1 Like

Here you go:

x = 58 
print(math.round(x/15)*15)

60

Explanation for future developers look at this question:

When you want to round something by a decimal, you typically do this:

math.round(x*10)/10

Taking a number like 12.125, it temporarily takes this number and makes it 121.25. When you round this number, it becomes 121. After dividing it, it becomes 12.1. In this way, we have rounded the number to the nearest decimal.

You can also round to the nearest 10s or 100s place by dividing first then multiplying. I simply took this application and did it for numbers of 15 increments. If you do the math out step by step, it’s easy to see why it works.

5 Likes

Thanks. I also just figured it out myself a few second ago, xd