Calculating angle magnitude difference between two points

Right now I’m making a terrain climbing system for my game and it works pretty smoothly except that the player rotates to every small budge it sees in the terrain, even if it’s the tiniest of angle difference.

Here’s a visualization of the bug;


As you can see, it looks pretty awkward, hence I’d like to change it.

So I would like the player to only rotate when there’s a difference of at least 5 degrees, however, I have no idea how to calculate the difference of angles, even after looking at other posts.

-- Current code:
while true do wait(0.05)
	local hit,pos,surface = SurfaceCheck() -- I'm using raycast.
	humanoidRootPart.CFrame = CFrame.new(pos,pos + surface)
	humanoidRootPart.CFrame = humanoidRootPart.CFrame * CFrame.new(0,0,-2) * CFrame.Angles(math.rad(0),math.rad(180),math.rad(0))	
end

Thank you for reading :slight_smile:

2 Likes

Something like this

local ANGLE_OFFSET = CFrame.Angles(math.rad(0),math.rad(180),math.rad(0))
local MIN_ANGLE_TO_UPDATE = math.rad(5)
locall lastSurfaceNormal
while true do wait(0.05)
	local hit,pos,surface = SurfaceCheck() -- I'm using raycast.
        local angleToLastNormal
        if lastSurfaceNormal then
            --Assumes surface is always a unit vector
            angleToLastNormal = math.acos(lastSurfaceNormal:Dot(surface))
        end
        if angleToLastNormal == nil or angleToLastNormal >= MIN_ANGLE_TO_UPDATE then
            humanoidRootPart.CFrame = CFrame.new(pos,pos + surface)
	    humanoidRootPart.CFrame *= CFrame.new(0,0,-2) * ANGLE_OFFSET
        end
	lastSurfaceNormal = surface
end
1 Like