How do I make the camera point towards the mouse

So I am trying to create something like to amazing play screen in:
https://www.roblox.com/games/3214114884/Flag-Wars?refPageId=2c3c3715-fd4e-4564-aba8-5f04a90c7c9d

I have got the buttons right(kind of).

But I am trying to make the camera move to where the mouse is pointing.

The issue is that it kind of points down to much and also it glitches out a lot when you go to the edges of the screen.

Here is what I tried:

CameraPart.CFrame = CFrame.new(CameraPart.Position, -(CameraPart.Position - Mouse.hit.p).Unit * 20)
2 Likes

Is this what you’re looking for?
You can adjust the sensitivity if needed since it might be quite jerky.

CurrentCamera.CFrame = CFrame.lookAt(
    Vector3.new(0, 10, 0), -- Position Vector3
    Player:GetMouse().Hit.Position -- lookAt Vector3
)
1 Like

That is what I did at first but It was way to fast. How would I adjust the sensitivity?

You can interpolate the camera’s CFrame every frame instead of instantly setting it:

game:GetService('RunService').RenderStepped:Connect(function()
    workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame:Lerp(
        CFrame.lookAt(
            Vector3.new(0, 10, 0), -- Position Vector3
            Player:GetMouse().Hit.Position -- lookAt Vector3
        ),
        0.1 -- must be between 0 and 1! to reduce sensitivity, try reducing the number (this is already 10% of the original speed)
    )
end)
3 Likes

I tried this at first but I still have the same problem, when you look up or down you camera spins a lot.

You’ll have to limit the amount of rotation on each individual axis. I was working on a project some time ago that used this same feature. The only thing I did different is that instead of lerping I tweened the camera’s CFrame:

local interpolationTime = 0.2 -- reduce sensitivity
local curMouseHit = mouse.Hit.Position

if userInputService:GetMouseLocation() ~= lastMousePos then
    lastMousePos = userInputService:GetMouseLocation()
    -- if you want the camera to always follow the mouse even when it isn't being moved, just comment out the line above
    local forwardVector = (cam.CFrame.Position - curMouseHit).Unit
    local upVector = Vector3.new(0, 1, 0)
    local rightVector = forwardVector:Cross(upVector)
    local upVector2 = rightVector:Cross(forwardVector)
    
    local newCFrame = CFrame.fromMatrix(cam.CFrame.Position, -rightVector, upVector2)
    
    local curX, curY, curZ = newCFrame:ToOrientation()
    
    if math.deg(curY) > 0 then
        curY = -curY
    end
    
    local xLimit = math.clamp(math.deg(curX), -40, -10) -- customize the clamping values. These are specific to how the camera I was using is rotated, and will most likely be different for yours
    local yLimit = math.clamp(math.deg(curY), -151.47, -118.53)
    local zLimit = math.clamp(math.deg(curZ), -6.88, 6.88)
    
    newCFrame = CFrame.new(newCFrame.Position) * CFrame.fromOrientation(math.rad(xLimit), math.rad(yLimit), math.rad(zLimit))
    
    local camInterpolation = tweenService:Create(
            cam,
            TweenInfo.new(interpolationTime, Enum.EasingStyle.Sine, Enum.EasingDirection.In),
            {
                    CFrame = newCFrame
            }
    )
	
    camInterpolation:Play()
end
1 Like

How is the xyzLimit set?

For example I want the camera to only be able to make 45 degrees up and down and side to side.

How would I do this?

You’d have to get the default rotation of the camera position you’re using. Then just add or subtract 45 degrees to appropriate axis. I might work on a better solution, and if I do I’ll update the post and let you know :slight_smile:

1 Like