Dynamic Top Down Camera Weird behaviour

So, I am making a dynamic top down camera (Where camera moves according to your mouse position) and I think I am close to achieving it but The camera is showing weird behaviour

(I recorded a video but my pc is so trash it’s literally at 1 fps so you can check it out at the place below)

Here’s the script

--Made by Beastcraft_Gaming

--Variables--
local player: Player = game.Players.LocalPlayer;
local character: Model = player.Character or player.CharacterAdded:Wait();
local humPart = character:WaitForChild("HumanoidRootPart");
local head = character:WaitForChild("Head");

local mouse = player:GetMouse();
local mousePosition

local camera = workspace.CurrentCamera;
local camHeight = script:GetAttribute("Cam_Height");

local maxDist 
--Main

camera.CameraType = Enum.CameraType.Scriptable;
--camera.CameraSubject = humPart;

while true do
    
    camHeight = script:GetAttribute("Cam_Height");
    maxDist = script:GetAttribute("Max_Dist");
    mousePosition = mouse.Hit;
    
    --distance between character and mouse
    local dist = (head.Position - mousePosition.Position).Magnitude;
    
    --clamping the dist
    dist = math.clamp(dist, 0, maxDist);
    
    --Offset vector
    local x = mousePosition.Position.Unit.X * dist ;
    local z = mousePosition.Position.Unit.Z * dist ;
    
    local OffsetVector = Vector3.new(x, camHeight, z);
    
    camera.CFrame = CFrame.new(head.Position + OffsetVector);
    camera.CFrame = camera.CFrame * CFrame.Angles(math.rad(-90),0,0);
    
    game:GetService("RunService").Heartbeat:Wait();
end

Update: I manage to fix it.

My method of clamping the Vector was wrong…

I came across this post which showed the correct way

Here is the final updated script

--Made by Beastcraft_Gaming

--Variables--
local player: Player = game.Players.LocalPlayer;
local character: Model = player.Character or player.CharacterAdded:Wait();
local humPart = character:WaitForChild("HumanoidRootPart");
local head = character:WaitForChild("Head");

local mouse = player:GetMouse();
local mousePosition

local camera = workspace.CurrentCamera;
local camHeight = script:GetAttribute("Cam_Height");

local movementOffset

local maxDist 
--Main

camera.CameraType = Enum.CameraType.Scriptable;
--camera.CameraSubject = humPart;

while true do
    
    camHeight = script:GetAttribute("Cam_Height");
    maxDist = script:GetAttribute("Max_Dist");
    mouseCFrame = mouse.Hit;
    
    movementOffset = script:GetAttribute("Mov_Offset");
    
    --distance between character and mouse
    local dist = (mouseCFrame.Position - humPart.Position).Magnitude
    local unitVector = (mouseCFrame.Position - humPart.Position).Unit;
    
    local offsetVector = unitVector * math.min(dist, maxDist) * movementOffset;
    
    camera.CFrame = CFrame.new(humPart.Position + Vector3.new(offsetVector.X,camHeight, offsetVector.Z)) * CFrame.Angles(math.rad(-90),0,0);
    
    game:GetService("RunService").Heartbeat:Wait();
end 
1 Like