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
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.
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)
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)