Roblox Studio camera system

This script is a system that makes the Camera (workspace.CurrentCamera) moving depending on Player:GetMouse() interactions. (As in Roblox Studio, when right-clicking your mouse, you can move your camera properly and indicate with your mouse where you want your camera to look at).

UIS.InputChanged:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseMovement then
        if moveCam then --Determined if right click is pushed or not, in another function and is only toggled if the condition is true
            UIS.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
            Camera.CFrame = Camera.CFrame * CFrame.Angles(math.rad(-input.Delta.y/rotateRatio), math.rad(-input.Delta.x/rotateRatio),0) --Does change camera rotation but weirdly -ç-
       else
            UIS.MouseBehavior = Enum.MouseBehavior.Default
       end
end

The result: https://gyazo.com/75cec296da0272511d9caa9801dd2e53

Do you know what can be the cause of all this?

Just to clarify, you want the camera to behave the same in game as in studio?

Yes I want the camera to work exactly the same as in Studio !

Okay. In the video, is the problem that the camera is rotating upside down? Or is it the entire shaking camera

The problem is that when I am rotating the camera, it does not remain parallel to the “floor” like, I am making the camera shaking in the video and when you do that, it makes it rotate weirdly and do this upside down effec (https://gyazo.com/4f947cd449426347bb69fa333a5041f0 I don’t know if it is clearer with this one)

Ohhh! I got it now. The problem is that you are setting the Z value to 0 only once every time the mouse is being held down. You need to add a RenderStepped event after if moveCam then

You could also just put the CFrame changer in a while wait(.05) do loop

In any way you want, you need to make sure at least that “0” is looped while the mouse is being held down.

Well uhm, I don’t really get it ngl, I though tried to do what you are saying but it seems that it does not work properly ._.

Hmm. I really don’t get it either.

I can’t really think of anything

Maybe adding

while wait(.05) do
    Camera.CFrame = CFrame.new(1, 1, 0)
end

At the end of the

Camera.CFrame = Camera.CFrame * CFrame.Angles...

Tbh though, I’m not to sure how to fix this tho :confused:

Well this makes my camera moving upper and towards Z .-.

Yeah. I’m not to sure either.

Sorry bout that. I’m not too good with CFrames lol

There is absolutely no problem about that ^^
Actually, I am also not that good when I have to manipulate them x)
Well, thank you to have spent a bit of your time to help me though, if you have any problem I may help you (Well, not on CFrames -) lmaoo

1 Like

A huge tanks to @mariokirby918 !!

I talked to him on Discord and he found a solution:

local X = 0
local Y = 0
UIS.InputChanged:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch or input.UserInputType == Enum.UserInputType.Gamepad2 then
        if moveCam then
            UIS.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition

            X += input.Delta.X
            Y += input.Delta.Y
            Camera.CFrame = CFrame.new(Camera.CFrame.Position) * CFrame.Angles(0,math.rad(-X),0) * CFrame.Angles(math.rad(-Y),0,0) 
        else
            UIS.MouseBehavior = Enum.MouseBehavior.Default
        end
    end
end)

I added something more to his script since the speed is quite fast, replace the line:

Camera.CFrame = CFrame.new(Camera.CFrame.Position) * CFrame.Angles(0,math.rad(-X),0) * CFrame.Angles(math.rad(-Y),0,0)

with this:

local rotateRatio = 20
Camera.CFrame = CFrame.new(Camera.CFrame.Position) * CFrame.Angles(0,math.rad(-X/rotateRatio),0) * CFrame.Angles(math.rad(-Y/rotateRatio),0,0)

The variable rotateRatio can be put somewhere else in the script (not in another function, the scope makes it not detectable)

So yea, I hope it will help tons of people that are searching solutions for the same issue than I had

Have a good day !

2 Likes