I’m trying to achieve a building map camera. Every single time my mouse is on top of the screen the part starts glitching. I’ve tried countless times to divide it by numbers, but still got the same issue. Any reason why?
RunService.RenderStepped:Connect(function()
CurrentCamera.CFrame = BuilderCam.CFrame
if holding_rightClick then
local MouseVector = Mouse.Hit.Position
local FinalVector = Vector3.new(MouseVector.X, MouseVector.Y, MouseVector.Z)
BuilderCam.CFrame = CFrame.lookAt(BuilderCam.Position, FinalVector)
end
end)
The issue might be caused by the camera trying to look at invalid positions when the mouse is near the edge of the screen. We can resolve this by checking if the mouse is on the screen before updating the camera CFrame. Here’s a modified version of your code:
RunService.RenderStepped:Connect(function()
CurrentCamera.CFrame = BuilderCam.CFrame
if holding_rightClick then
local MouseVector = Mouse.Hit.Position
local FinalVector = Vector3.new(MouseVector.X, MouseVector.Y, MouseVector.Z)
-- Check if the mouse is on the screen
if Mouse.X > 0 and Mouse.X < workspace.CurrentCamera.ViewportSize.X and
Mouse.Y > 0 and Mouse.Y < workspace.CurrentCamera.ViewportSize.Y then
BuilderCam.CFrame = CFrame.lookAt(BuilderCam.Position, FinalVector)
end
end
end)
This should prevent the camera from glitching when the mouse is at the edge of the screen.
I made the changes to add a condition that checks if the mouse is within the screen boundaries before updating the camera’s CFrame. The main issue you were facing was the camera glitching when your mouse was at the top of the screen, which I assumed was caused by the camera trying to look at invalid positions due to the edge of the screen.
The added condition checks if the mouse’s X and Y positions are within the viewport size of the workspace’s current camera. This ensures that the camera is only updated when the mouse is within the screen boundaries, preventing the camera from glitching when the mouse is near the edge of the screen.
By doing this, the camera will only update its CFrame when the mouse is within a valid position on the screen, which should resolve the glitching issue you were experiencing.
I have pasted the code into my script. It is still causing the same issues. The issue isn’t the part glitching when the mouse is out of the screen. It glitches when your cursor is pointed up too much causing the part to spin here is another video.
Mabye the issue is caused due to the camera’s pitch reaching extreme angles when the cursor is pointed up. To fix this clamp the pitch angle within a certain range. Here’s an updated version of the code:
local function calculatePitchAngle(camera, target)
local directionVector = (camera.Position - target).Unit
local _, pitch = camera.CFrame:ToOrientation()
return pitch
end
RunService.RenderStepped:Connect(function()
CurrentCamera.CFrame = BuilderCam.CFrame
if holding_rightClick then
local MouseVector = Mouse.Hit.Position
local FinalVector = Vector3.new(MouseVector.X, MouseVector.Y, MouseVector.Z)
local minPitchAngle = -math.pi / 2.5
local maxPitchAngle = math.pi / 2.5
local currentPitchAngle = calculatePitchAngle(BuilderCam, FinalVector)
if currentPitchAngle > minPitchAngle and currentPitchAngle < maxPitchAngle then
BuilderCam.CFrame = CFrame.lookAt(BuilderCam.Position, FinalVector)
end
end
end)
This code calculates the current pitch angle of the camera using the calculatePitchAngle function. It then checks if the current pitch angle falls within the defined range (minPitchAngle and maxPitchAngle). The camera’s CFrame is only updated if the current pitch angle is within the acceptable range, thus preventing the part from spinning when the cursor is pointed up too much.
It seems like there is a problem with the way I am clamping the pitch angle. Here’s an alternative approach to fix the issue:
local function calculatePitchAngle(camera, target)
local directionVector = (camera.Position - target).Unit
local _, pitch = camera.CFrame:ToOrientation()
return pitch
end
RunService.RenderStepped:Connect(function()
CurrentCamera.CFrame = BuilderCam.CFrame
if holding_rightClick then
local MouseVector = Mouse.Hit.Position
local FinalVector = Vector3.new(MouseVector.X, MouseVector.Y, MouseVector.Z)
local newCFrame = CFrame.lookAt(BuilderCam.Position, FinalVector)
-- Calculate pitch angle
local pitch = calculatePitchAngle(newCFrame, FinalVector)
-- Clamp pitch angle
local minPitchAngle = -math.pi / 2.5
local maxPitchAngle = math.pi / 2.5
if pitch > minPitchAngle and pitch < maxPitchAngle then
BuilderCam.CFrame = newCFrame
end
end
end)
This code calculates the pitch angle after constructing the new CFrame. It then clamps the pitch angle by checking if it falls within the defined range (minPitchAngle and maxPitchAngle). The camera’s CFrame is only updated if the pitch angle is within the acceptable range, thus preventing the camera from spinning when the cursor is pointed up or down too much.
local function calculatePitchAngle(cameraCFrame, target)
local directionVector = (cameraCFrame.Position - target).Unit
local _, pitch = cameraCFrame:ToOrientation()
return pitch
end
changed calculatePitchAngle() function to accept a cframe instead of a camera
Changed the script so it uses the CFrame.Angles function to construct the camera’s CFrame directly from the Mouse’s XY coordinates while limiting the pitch angle. Here’s the updated code:
RunService.RenderStepped:Connect(function()
CurrentCamera.CFrame = BuilderCam.CFrame
if holding_rightClick then
-- Calculate yaw and pitch based on the mouse position in the viewport
local yaw = (Mouse.X / workspace.CurrentCamera.ViewportSize.X - 0.5) * 2 * math.pi
local pitch = (Mouse.Y / workspace.CurrentCamera.ViewportSize.Y - 0.5) * math.pi
-- Clamp pitch angle
local minPitchAngle = -math.pi / 2.5
local maxPitchAngle = math.pi / 2.5
pitch = math.clamp(pitch, minPitchAngle, maxPitchAngle)
-- Construct the camera's CFrame using yaw and pitch
local lookDirection = CFrame.Angles(pitch, yaw, 0) * Vector3.new(0, 0, -1)
BuilderCam.CFrame = CFrame.new(BuilderCam.Position, BuilderCam.Position + lookDirection)
end
end)
This code calculates the yaw and pitch angles based on the mouse position in the viewport, then clamps the pitch angle using math.clamp. The camera’s CFrame is updated using the CFrame.Angles function, which constructs a new CFrame from the yaw and pitch angles.
I learned this from a lot of time spent in studio, reading the developer docs from roblox, and having a mentor. Would highly recommend these steps to becoming a better programmer.