How would i make my camera follow my cursor slightly

heylo again guys ive been working on my game and have made great process but i wanted to add 1 last effect to apply for everything how do i make the camera slightly follow the mouse for a certain angle?
this is my camera script down below

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local CurrentCamera = workspace.CurrentCamera

CurrentCamera.CameraType = Enum.CameraType.Scriptable
workspace.Camera.CameraType = Enum.CameraType.Custom
wait()
workspace.Camera.CameraType = Enum.CameraType.Scriptable
CurrentCamera.CFrame = workspace.CameraPart.CFrame

and this is the picture of how i thiunk i can best describe it


the blue like is like the max the camera can turn to while following the mouse
any idea on how to?

Heres the game if you want to check it out!

21 Likes

You can use Mouse.Hit to get the mouse’s 3D position in the world, then set the cframe of the camera.

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local CurrentCamera = workspace.CurrentCamera

CurrentCamera.CameraType = Enum.CameraType.Scriptable
workspace.Camera.CameraType = Enum.CameraType.Custom
wait()
workspace.Camera.CameraType = Enum.CameraType.Scriptable


game:GetService("RunService").Heartbeat:Connect(function(dt)
    CurrentCamera.CFrame = CFrame.new(CurrentCamera.CFrame.p, Player:GetMouse().Hit) -- keep its position, but change where it is looking
end)

Note I didn’t test this in studio, so there may be an error! Please respond to me if there is.

2 Likes

alrighty i will do!
thanks for the help

1 Like

where would i put the script and set the c frame to the part?

Hey, I tested my code in Studio and of course it doesn’t work. I’m currently rewriting it, sorry!

1 Like

oh its fine im just fixing animations with the little noob!

Here is another way to do it without needing a mouse target or hit. This is useful for if you have target filters or have your mouse pointing at the sky, as mouse.Hit might return a crazy number or just plain old nil.

You can use the ViewSize of mouse to get the Vector2 position on your screen (basically UDim2.fromOffset) that your mouse is in.

Anyway, here is the code of how you would implement it:

--// Variables
local camPart = workspace["Camera Part Goes Here"]
local mouse = game:GetService("Players").LocalPlayer:GetMouse()

--// Set cam
repeat
      wait()
      cam.CameraType = Enum.CameraType.Scriptable
until
      cam.CameraType == Enum.CameraType.Scriptable

--// Move cam
local maxTilt = 10
game:GetService("RunService").RenderStepped:Connect(function()
      cam.CFrame = camPart.CFrame * CFrame.Angles(
            math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),
            math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),
            0
      )
end)
11 Likes

so id just put it in here correct?

1 Like

You could put it there, but I normally put my camera scripts in StarterGui.

LocalScripts run in StarterPlayerScripts (Player), StarterCharacterScripts (Character), StarterGui (PlayerGui), and tools (Backpack/Character)

3 Likes

yeah i just thought it would run better for 1 player all together then just it being server wide

It is still for one player either way.

2 Likes

14:15:35.932 Camera Part Goes Here is not a valid member of Workspace “Workspace” - Client - Camera:2

1 Like

You’re suppose to replace it with the object you want (Camera Part)

2 Likes

so i would just rename cam to the part i need it to?

The error says Camera Part Goes Here is not a valid member of Workspace meaning that, well… “Camera Part Goes Here” does not exist in the workspace. The part where it says Camera:2 means that it is from the script called “Camera” and on the second line, meaning that it is from the part saying local camPart = workspace[“Camera Part Goes Here”]

In my code, I put that there so you can put your own camera part there. You can do that by adding a new part to the workspace and positioning it where you want the camera to be.

If that seems too difficult or long, I made a plugin specifically for this: Camera Part Maker

The final step is changing the second line to local camPart = workspace.CamPart or replace CamPart with whatever you called the part. (The plugin defaults to CamPart)

3 Likes

im afraid i dont understand the problem still i just lost my script and previous camera part and the script is still giving me the same error

--// Variables
local camPart = workspace["Camera Part Goes Here"]
local mouse = game:GetService("Players").LocalPlayer:GetMouse()

--// Set cam
repeat
      wait()
      cam.CameraType = Enum.CameraType.Scriptable
until
      cam.CameraType == Enum.CameraType.Scriptable

--// Move cam
local maxTilt = 10
game:GetService("RunService").RenderStepped:Connect(function()
      cam.CFrame = camPart.CFrame * CFrame.Angles(
            math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),
            math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),
            0
      )
end)

The part where it says Camera Part Goes Here needs to be renamed to the name of your camera part

4 Likes

and what about the parts that say cam?

--// Variables
local cam = workspace.CurrentCamera
local camPart = workspace["CameraPart"]
local mouse = game:GetService("Players").LocalPlayer:GetMouse()

--// Set cam
repeat
      wait()
      cam.CameraType = Enum.CameraType.Scriptable
until
      cam.CameraType == Enum.CameraType.Scriptable

--// Move cam
local maxTilt = 10
game:GetService("RunService").RenderStepped:Connect(function()
      cam.CFrame = camPart.CFrame * CFrame.Angles(
            math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),
            math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),
            0
      )
end)
63 Likes

thanks for the help i appreciate it alot it looks really nice now

7 Likes