Part Vector Issue

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?

Video: https://gyazo.com/a5a6b13565b7173d2a095fdc74c5f8da

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)
2 Likes

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.

1 Like

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.

https://gyazo.com/bbc9a524d74a60f68a13536cf3bae228

2 Likes

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.

1 Like

The camera isn’t even moving anymore. I’m not sure what the exact problem is because I’m not experienced in Roblox math.

1 Like

Apologies for the inconvenience. I have revised the code to ensure the camera moves while also limiting the pitch angle. Here’s the updated version:

local function clampPitchAngle(camera, target)
    local horizontalDirection = (Vector3.new(target.X, camera.Position.Y, target.Z) - camera.Position).Unit
    local verticalDirection = (target - camera.Position).Unit

    local pitchLimit = math.pi / 2.5
    local pitch = math.asin(verticalDirection.Y)

    if pitch > pitchLimit then
        verticalDirection = Vector3.new(0, math.sin(pitchLimit), 0) + horizontalDirection * math.cos(pitchLimit)
    elseif pitch < -pitchLimit then
        verticalDirection = Vector3.new(0, math.sin(-pitchLimit), 0) + horizontalDirection * math.cos(-pitchLimit)
    end

    return camera.Position + verticalDirection
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 clampedTarget = clampPitchAngle(BuilderCam, FinalVector)
        BuilderCam.CFrame = CFrame.lookAt(BuilderCam.Position, clampedTarget)
    end
end)
1 Like

Hooray it works. The only issue I have left is it’s spinning here is what I am talking about.
https://gyazo.com/839b25e0ddae30e8ceeaec69038e391d

1 Like

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.

1 Like

22:33:55.021 CFrame is not a valid member of CFrame - Client - BuildCam:52

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

1 Like

It’s really buggy. If this doesn’t work it’s fine. Under here is the video, and a question how did you learn this advance roblox programming?

https://gyazo.com/50cc98bb2ff4d0af991114b16f91bdda

1 Like

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.

1 Like

It works. Thanks for helping out!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.