I’m making a custom camera lock system for a third person gun, and for the most part the system works, but for some reason the character seems a little glitchy and rotates weirdly.
While its nothing major, it is a little annoying.
Basically, I just lock the mouse in the center, and set the character CFrame to use the camera LookVector every frame.
local Player = game.Players.LocalPlayer
local Character = script.Parent
Character.Humanoid.CameraOffset = Vector3.new(2, .5, 0)
game:GetService("RunService").RenderStepped:Connect(function()
if Character:FindFirstChild("HumanoidRootPart") then
if Character.Humanoid.Health > 1 then
local CamLook = workspace.CurrentCamera.CFrame.LookVector
local RootFrame = Character.HumanoidRootPart.CFrame
local FlatLook = Vector3.new(CamLook.X, 0, CamLook.Z)
Character.HumanoidRootPart.CFrame = CFrame.new(RootFrame.p, RootFrame.p + FlatLook)
end
end
game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.LockCenter
end)
I’ve found that leaving the Humanoid CameraOffset as (0, 0, 0) fixes it, but I need the camera offset. Maybe I need to subtract that Vector that I use for camera offset from something, but anytime I try that it bugs out so not sure.
So I fixed it by not using CameraOffset, and instead editing the camera CFrame to be in the right position.
local Player = game.Players.LocalPlayer
local Character = script.Parent
local cameraAngleX = 0
local cameraAngleY = 0
local function playerInput(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Change then
cameraAngleX = cameraAngleX - inputObject.Delta.X
cameraAngleY = math.clamp(cameraAngleY-inputObject.Delta.Y*0.4, -75, 75)
Character.HumanoidRootPart.CFrame = Character.HumanoidRootPart.CFrame * CFrame.Angles(0, math.rad(-inputObject.Delta.X), 0)
end
end
game:GetService("ContextActionService"):BindAction("PlayerInput", playerInput, false, Enum.UserInputType.MouseMovement, Enum.UserInputType.Touch)
game:GetService("RunService").RenderStepped:Connect(function()
if Character:FindFirstChild("HumanoidRootPart") then
if Character.Humanoid.Health > 1 then
local CamLook = workspace.CurrentCamera.CFrame.LookVector
local RootFrame = Character.HumanoidRootPart.CFrame
local FlatLook = Vector3.new(CamLook.X, 0, CamLook.Z)
local StartFrame = CFrame.new((Character.HumanoidRootPart.CFrame.Position)) * CFrame.Angles(0, math.rad(cameraAngleX), 0) * CFrame.Angles(math.rad(cameraAngleY), 0, 0)
local CamFrame = StartFrame:ToWorldSpace(CFrame.new(2, 2, 5))
local CamFocus = StartFrame:ToWorldSpace(CFrame.new(2, 2, -10000))
workspace.CurrentCamera.CFrame = CFrame.new(CamFrame.Position, CamFocus.Position)
Character.HumanoidRootPart.CFrame:Lerp(CFrame.new(RootFrame.p, RootFrame.p + FlatLook), 1.1)
end
end
game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.LockCenter
end)