The way to round to the nearest 'x' number is to use the following algorithm, where 'number' is the original number, and 'multiple' is the number being rounded toward (e.g. 3):
floor( number + (multiple * 0.5) / multiple ) * multiple
As Lua code in the form of a function, it could look like this:
function RoundMultiple(n, mult)
return math.floor((n + (mult * 0.5)) / mult) * mult
end
To do this with a Vector3, we just need to write another function that calls 'RoundMultiple' for each X, Y, and Z property of a Vector3. Thus, to answer your question, a finalized function might look like this:
function RoundVector3(v3, mult)
local x = RoundMultiple(v3.X, mult)
local y = RoundMultiple(v3.Y, mult)
local z = RoundMultiple(v3.Z, mult)
return Vector3.new(x, y, z)
end
And you could call it like this:
local v3 = Vector3.new(30, 23, 21)
local v3Rounded = RoundVector3(v3, 3) -- Round all the values to the nearest multiple of '3' |