How do I make my Camera slightly follow my Mouse (On and Off)

Hello!

While developping my game, I thought it would be a good idea to have the camera slightly follow the mouse. Expet, my game is a FNAF fan game, yes there are cameras. So basically here’s what I am trying to do.

local CurrentCam = game.Workspace.CurrentCamera
local CameraPart = game.Workspace.CameraPart
--Basically we get our basics.
-- Also, we will check if we're on PC or not.
--Also I have already a basic script

    --So this is what we do when ON
--The ON/OFF thing I can take care of.

--ON
--Make it follow the mouse

--To change we probably will just have a function of whether we change the Properties.

OFF-- 
--Not following the mouse.

If you want to know more here’s my game.

Thanks for reading.

2 Likes

If what you are going for is a Parallax Effect then here is a previous post on the same thing: Creating a parallax effect

2 Likes

Basically, I tried to do the follow mouse thing but if I change CameraSubject, it doesn’t change. So I need to have a BoolValue (this I can easily handle) that changes when we want the camera to follow the mouse or not.

Value:GetPropertyChangedSignal("Value"):Connect(function()
if Value.Value == false then
--If we for example are using the security cameras.
--Remove the effect in a way
else if Value.Value == true then
--The effect here
end

end)

You get what I am referring to here.

So here’s my basic script (local)

local Camera = workspace.CurrentCamera

local Run = game:GetService("RunService")


wait()

workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable

workspace.Camera.CameraType = Enum.CameraType.Custom

wait()

Camera.CameraType = Enum.CameraType.Scriptable



Camera.CameraSubject = workspace.CameraPart

Run:BindToRenderStep("CameraSubject", Enum.RenderPriority.Camera.Value, function()

if Camera.CameraSubject and Camera.CameraSubject:IsA("BasePart") then

Camera.CFrame = Camera.CameraSubject.CFrame

end

end)

Please tell me anyone of if my examples would be correct.

1 Like

The way I did camera orientation in my game engine is I had it so it would update the viewMatrix (workspace.Camera.CFrame)'s 3x3 rotational/scaling component with these factors

camerafront = Vector3.new(
                          math.cos(math.rad(yaw)) * math.cos(math.rad(pitch)),
                          math.sin(math.rad(pitch)),
                          math.sin(math.rad(yaw)) * math.cos(math.rad(pitch))
                          )

Don’t tell me how the math works, i stole it but what I do know is I would set the yaw to be the difference in pixels that the mouse’s x moved, and pitch for the mouse’s delta Y. from there I just calculated the rest of the orthogonal vectors like usual, random vector for right, cross that with the front thats the right vector, the up vector is the cross of those and there ya go, you got 3 vector components which specify the front, up and right vector. Now all you gotta do is just check if yaw > 70 or yaw < -70 and pitch > 70 or pitch < -70 then and then clamp those in a sense. From there I had to look at the wiki for this but you’d use fromMatrix after

1 Like

Hm maybe this would help. Have you tried it before?

Yea but the codes in c++ is the reason why

So is it possible to turn it off, as mentioned in my example.

By your example, I forgot to say this, it seems it’s not for CameraSubjects, which is not what I am looking for.

Yea you’d just stop updating the cframe

Hey well, while writing my script, I wasn’t able to turn the effect off.

Code:
local Value = game.Workspace.Conditions.Player.CamsOff
Value:GetPropertyChangedSignal(“Value”):Connect(function()
if Value.Value == false then
–If we for example are using the security cameras.
–Remove the effect in a way
else if Value.Value == true then
–When Cameras are off
–// 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)
end
–The effect here
end
end)