I am making a custom camera for my horror game but the camera keeps glitching, this is the code i am currently using:
--Camera Manipulation
local X, Y, AngleX, AngleY = 0,0, CFrame.Angles(0,0,0), CFrame.Angles(0,0,0) -- Base values
local Offset = CFrame.new(0, 0.25, 1.25) * CFrame.Angles(0, math.rad(180), 0) -- You might want to change this to suit your game better
function Update()
head.CanCollide = false
uis.MouseBehavior = Enum.MouseBehavior.LockCenter
camera.CameraType = Enum.CameraType.Scriptable
local Delta = uis:GetMouseDelta() -- Get change in mouse position
X = X + (Delta.X*0.15) -- Set X value accordingly (mouse left/right)
Y = math.clamp(Y + (Delta.Y*0.15),-89.99,89.99) -- Set value Y value accordingly (mouse up/down, clamped to prevent glitch where camera rotates at 90)
AngleX = CFrame.Angles(0, math.rad(-X), 0) -- Create angles using X on Y
AngleY = CFrame.Angles(math.rad(-Y),0,0) -- Create angles using Y on X
camera.CFrame = CFrame.new((character.Head.CFrame).p) * AngleX * Offset * AngleY
local Position = character.HumanoidRootPart.CFrame.p -- Set to the root part's position
local GoalRotate = CFrame.new(Position, camera.CFrame.Position) -- Faces the camera from the root part's position
local _, RotY, _= GoalRotate:ToOrientation() -- Stores the Y orientation value, which we will then use in CFrame.fromOrientation to set the X and Z to 0, limiting it to Y (rotating side to side)
character.HumanoidRootPart.CFrame = CFrame.new(Position) * CFrame.fromOrientation(0, RotY, 0)
end
--RunService
rs:BindToRenderStep("CameraUpdate",Enum.RenderPriority.Camera.Value, Update)
(I set head CanCollide Property to false because i thought that was the problem)
Video: