How to stop this weird value flipping?

I’ve been trying to fix my rotation for a small boat project. I have a boat the floats up and down with the waves, but it doesn’t rotate with them. Currently, I get this weird effect where the value is what it should be, but every other frame it’s flipped.
Current code is: (v is the boat model, front & back are parts that go up and down with the water)

local pos1 = primaryPart:FindFirstChild('Front').Position
local pos2 = primaryPart:FindFirstChild('Back').Position
local heightDif = pos1.Y - pos2.Y
local distance = (pos1 - pos2).Magnitude
local angle = math.atan(heightDif / distance)
angle = math.deg(angle)
print(angle)

v:SetPrimaryPartCFrame(v.PrimaryPart.CFrame * CFrame.Angles(math.rad(angle), 0, 0))

The output looks like this:
image

Thanks for any help

1 Like

Try to set the second angle set thing to an other variable, and change all the variables to it prints it correct and stuff

If you want the value to not be negative you use math.abs