Help lerping the neck motor6d

I am making a wall climbing system and I want the players head to face in the direction they are moving. I have that working but it only sets the C0, I want it to smoothly transition so I tried to use a lerp but I think I’m doing it wrong.

Here is a bit of the class I made to update the player head.

HeadMover.ConstraintAngles = Vector3.new(math.rad(30), math.rad(30), 0)

--relevant methods
function HeadMover:UpdateNeck(direction: Vector3, dt) --fired every runservice.Stepped
        --the direction is the movement direction relative to the player primary part
	self.head.CanCollide = false --not sure a better way to make the head not collide with the wall
	if not direction or direction == Vector3.new() then
		direction = self.hrp.CFrame.LookVector
	end
	direction = direction.Unit
	local RotationOffset = (self.char.PrimaryPart.CFrame- self.char.PrimaryPart.CFrame.p):inverse()
	local DesiredHeadCF = RotationOffset * CFrame.new(Vector3.new(0,0,0), direction)
	
	local newNeckC0 = self:LerpNeck(DesiredHeadCF, dt)

	self.neck.Transform = newNeckC0
end

function HeadMover:LerpNeck(CF, dt)
	local X, Y = HeadMover.ConstraintAngles.X, HeadMover.ConstraintAngles.Y
	-- goal
	local x, y = CF:toEulerAnglesXYZ() -- goal angle
	local goalCF = CFrame.new(CF.p) * CFrame.Angles(
		math.clamp(x, -X, X),
		math.clamp(y, -Y, Y),
		0
	) -- constrain angles and construct cframe
	local interpolatedCF = self.neck.C0:Lerp(goalCF, dt) -- lerp
	return interpolatedCF
end

I’m not sure how to get the lerp to move smoothly. Thank you for your time.

I was able to figure this out. Basically, the Transform property doesn’t set the C0 property so the lerp was constant at 0.015(frame time).
To fix this I created a vector2(i don’t want z because that is the pivot) property to store the previous angle. I only lerp the vector2 and not the overall CFrame because that changed the position offset of the joint.

function HeadMover:LerpNeck(CF, dt)
local X, Y = HeadMover.ConstraintAngles.X, HeadMover.ConstraintAngles.Y
-- goal
local x, y = CF:toEulerAnglesXYZ() -- goal angle
local angleXY = Vector2.new(
	math.clamp(x, -X, X),
	math.clamp(y, -Y, Y)
)
local lerpedAngle = self.currentAngle:Lerp(angleXY, dt * 5) -- the 5 is my speed multiplier
self.currentAngle = lerpedAngle
local finalCF = CFrame.new(CF.p) * CFrame.Angles(
	lerpedAngle.X,
	lerpedAngle.Y,
	0
) -- constrain angles and construct cframe
return finalCF
end
1 Like