How To Round Angle to 90 Degrees

Hello, I’m trying to make a build system. Only problem is that the part rotates at 45 degrees. But, I want it at a 90 degree angle.

Image:

So as you can see when I rotate to a 45 degree angle it makes the wall a 45 degree angle, but I want it to be 90 degrees.

Script:

local rootCFrame = rootPart.CFrame
local angle = rootCFrame - rootPart.Position

local cframeTable = table.pack(angle:GetComponents())

for i, v in pairs(cframeTable) do
	cframeTable[i] = math.round(v)
end

angle = CFrame.new(table.unpack(cframeTable))
visual:PivotTo(angle + hitPos)

Thanks.

2 Likes

Then rotate to a 90 degree angle, and it makes the wall a 90 degree angle.
image

2 Likes

Wow! So how do I forcefully rotate the character to a multiple of 90?

1 Like

You could try getting the rotation on the y, then dividing it by 90 and rounding it before then multiplying it by 90 again. idk if this would work my brain is tired

2 Likes
-- I forgot the how Luau handles modulo's negative case, this should probably work
function roundRotation(rotation, radians)
    return (rotation - rotation % (2 * math.pi)) + rotation % radians
    -- Edit: Alternative code: return math.round(rotation / radians) * radians
end

function roundCFrameOrientation(cframe, radians)
    local rx, ry, rz = cframe:ToEulerAnglesXYZ()
    return CFrame.fromEulerAnglesXYZ(roundRotation(rx, radians), roundRotation(ry, radians), roundRotation(rz, radians)) + cframe.Position
end

-- 90 degrees example:
local cframe = -- Your CFrame
local rounded = roundCFrameOrientation(cframe, math.rad(90))
2 Likes

I ended up finding a solution. I basically did what you did, but out of 1. How does it work? I don’t know. Why did I use half of pi? I don’t know. Just testing and it works.

Script:

local x, y, z = rootPart.CFrame:ToOrientation()
local dividant = math.pi / 2
	y = math.round(y / dividant) * dividant
local newCFrame = CFrame.fromOrientation(x, y, 0) + hitPos

visual:PivotTo(newCFrame)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.