While the code works, there’s a significant problem that makes the character jitter while it rotates its head to my heads position (specifically Y axis). Anyone know a fix for this?
Here is the code:
RunService.Heartbeat:Connect(function()
if #playersInSight > 0 and not isMoving then
local playerName = playersInSight[1]
if playerName then
local PlrInstance: Player = Players:FindFirstChild(tostring(playerName))
if PlrInstance then
local Character = PlrInstance.Character
if Character then
local toLook: Part = Character:FindFirstChild("HumanoidRootPart")
if toLook then
local targetPos = Vector3.new(toLook.Position.X, head.Position.Y, toLook.Position.Z)
local lookAtCFrame = CFrame.lookAt(head.Position, targetPos)
head.CFrame = head.CFrame:Lerp(lookAtCFrame, lookAtLerp * 1.1)
end
end
end
if PlrInstance and PlrInstance.Character and PlrInstance.Character:FindFirstChild("HumanoidRootPart") then
local targetPosition = PlrInstance.Character.Head.Position + PlrInstance.Character.Head.Velocity / 8
local woolPart = woolMotor.Part1
local woolPosition = woolPart.Position
local direction = targetPosition - woolPosition
local lookVector = direction.Unit
local targetAngleY = math.atan2(lookVector.X, lookVector.Z)
local targetAngleX = math.asin(lookVector.Y)
targetAngleX = math.clamp(targetAngleX, -math.pi/6, math.pi/6) -- -30 to 30 degrees
targetAngleY = math.clamp(targetAngleY, -math.pi/6, math.pi/6) -- -45 to 45 degrees
local originalC0 = woolMotor.C0
local targetC0 = CFrame.new(originalC0.Position) * CFrame.Angles(targetAngleX, targetAngleY, 0)
woolMotor.C0 = originalC0:Lerp(targetC0, lookAtLerp)
end
end
else
local currentC0 = woolMotor.C0
woolMotor.C0 = currentC0:Lerp(originalC0, lookAtLerp)
end
end)