I have an issue with the CFrame.lookAt() function that seems to stutter or fitter the player movement. It seems like the position gets constantly realigned vertically with the y
Here’s my punching bag player movement update code :
local function OnUpdate()
if Target and LockOn then
Hum.AutoRotate = false
Hum.WalkSpeed = 0
UserInput.MouseBehavior = Enum.MouseBehavior.Default
local lookAt = HRP.CFrame:PointToWorldSpace(Vector3.new(0, 0, -3)):Lerp(MainPart.Position, .1)
HRP.CFrame = CFrame.lookAt(HRP.Position, lookAt, HRP.CFrame.UpVector)
local HumanoidPosition = char:GetPivot().Position
if LookingSide == 1 then
if UserInput:IsKeyDown(Enum.KeyCode.A) and theta > -2.5 then
theta -= 0.1
Hum.WalkSpeed = 16 + (Player:WaitForChild("CharStats").Speed.Value * 0.01)
KeyDown = true
HumanoidPosition = Vector3.new(MainPart.Position.X + radius*-math.sin(theta), HumanoidPosition.Y, MainPart.Position.Z + radius*-math.cos(theta))
Hum:MoveTo(HumanoidPosition)
end
if UserInput:IsKeyDown(Enum.KeyCode.D) and theta < 2.5 then
theta += 0.1
Hum.WalkSpeed = 16 + (Player:WaitForChild("CharStats").Speed.Value * 0.01)
HRP.CFrame = CFrame.lookAt(HRP.Position, lookAt, Vector3.new(0, 1, 0))
HumanoidPosition = Vector3.new(MainPart.Position.X + radius*-math.sin(theta), HumanoidPosition.Y, MainPart.Position.Z + radius*-math.cos(theta))
Hum:MoveTo(HumanoidPosition)
end
else
if UserInput:IsKeyDown(Enum.KeyCode.A) and theta > -2.5 then
theta -= 0.1
Hum.WalkSpeed = 16 + (Player:WaitForChild("CharStats").Speed.Value * 0.01)
HRP.CFrame = CFrame.lookAt(HRP.Position, lookAt, Vector3.new(0, 1, 0))
KeyDown = true
HumanoidPosition = Vector3.new(MainPart.Position.X + radius*math.sin(theta), HumanoidPosition.Y, MainPart.Position.Z + radius*math.cos(theta))
Hum:MoveTo(HumanoidPosition)
end
if UserInput:IsKeyDown(Enum.KeyCode.D) and theta < 2.5 then
theta += 0.1
Hum.WalkSpeed = 16 + (Player:WaitForChild("CharStats").Speed.Value * 0.01)
HRP.CFrame = CFrame.lookAt(HRP.Position, lookAt, Vector3.new(0, 1, 0))
HumanoidPosition = Vector3.new(MainPart.Position.X + radius*math.sin(theta), HumanoidPosition.Y, MainPart.Position.Z + radius*math.cos(theta))
Hum:MoveTo(HumanoidPosition)
end
end
else
theta = -2.5
Target = nil
LockOn = false
Hum.AutoRotate = false
--M2("On")
UserInput.MouseIconEnabled = true
end
end
I recommend using a bodygyro instead, you can feel free to use the same CFrame that you’re setting your HRP to, just make sure the bodygyro is under the HRP and the CFrame points to where-ever u want it to look
The most likely cause, just from glancing over your code, is that you have a feedback loop where your lookAt points the character towards some point which is computed in the local space of the HRP, so when the lookAt re-orients the HRP, that point also changes. Mild jitter is the least bad outcome, depending on the size of the correction, feedback problems can cause wild spinning (like a dog chasing its own tail) or vibrating that increases rapidly to chaos. The Lerp you have with the fixed world-space point is likely damping this and keeping it from blowing up.
The solution is to make sure the lookAt target point is not a function of the orientation of the part the lookAt is getting applied to.
I know that lookAt is a problem at best I tried using the Spring module for this but I just don’t know how to implement the Spring module with this issue
Alright with the help of BodyGyro character is jittering a little bit but it’s way better than earlier on since the character is still stucking at the same place for minutes unlike the start where we could see character naturally goes backward while not moving because of the CFrame stuttering
Keep in mind that with a standard Humanoid-based avatar the character has a balance gyro that is essentially infinite torque. You can’t counteract it with a BodyGyro or AlignOrientation, at best they will fight each other and jitter constantly. If your lookAt is trying to tilt the character’s root part away from vertical, even a tiny bit, the Humanoid balance gyro will correct them back to upright. If you do this every frame, it will just jitter. This could be the real cause of your problem.
If you need to tilt the character to point it at something, you can’t do it by tilting the HumanoidRootPart unless the character has been manually put into the Physics state, or PlatformStanding, Flying, etc. one of the states that disables the balance gyro. Because you lose a lot of basic movement functionality by doing this, it’s generally preferable to tilt the character by setting a rotational offset on the Root Motor6D using the Motor6D.Transform property. Root is the animatable joint that connects the HumanoidRootPart to the LowerTorso.