Some help with a... custom camera system

I need to create a custom camera system (to override the core camera system, due to certain features I need), however I am not the most efficient when it comes to Cameras, and less CFrames. So essentially I’ve tried this:

RunService:BindToRenderStep("RotateCamera", 50, function()
	local delta = UserInputService:GetMouseDelta()
	currentCamera.CFrame.Rotation = CFrame.Angles(delta.X, delta.Y, 0)
end)

And yeah, it errors (Rotation cannot be assigned to, probably because I don’t know how CFrames work). Don’t know what to do next. Again, I’m not really the best at CFrames.

The only thing I would like to achieve now is to get a camera that rotates as much as the mouse moves (delta), and I’ll do the rest, which is custom sensitivity.

Any advice appreciated, thanks.

2 Likes

currentCamera.CFrame.Rotation = CFrame.Angles(delta.X, delta.Y, 0)

you cannot assign it to CFrame.Rotation instead try this

currentCamera.CFrame *= CFrame.Angles(delta.X, delta.Y, 0)

1 Like

This will rotate the camera, but probably not how OP wants. First of all, the pitch axis (looking up/down) is the X axis, and yaw (side to side) is the Y axis, so the components should be swapped.

Second, that will rotate the camera relative to it’s current CFrame. Which is fine for pitching, since it will rotate the camera around it’s local X axis which is correct, but it won’t work for yaw. Imagine looking (pitching) 45 degrees up. Then the local Y axis isn’t straight up in world space, it’s half back and half up. To fix it, you need to instead rotate the camera around a different axis. That axis is the world Y axis but transformed to the camera’s space so it works correctly when applying the transformation.

Here’s how:

function handleMouseMovement(delta)
    --Handle pitch
    camera.CFrame *= CFrame.Angles(-delta.Y * 0.01, 0, 0)
    
    --Handle yaw
    local yawAxis = camera.CFrame:VectorToObjectSpace(Vector3.yAxis)
    camera.CFrame *= CFrame.FromAxisAngle(yawAxis, -delta.X * 0.01)
end

I’m p sure this should work, let me know if it doesn’t tho :I

3 Likes

The code:

RunService:BindToRenderStep("RotateCamera", 50, function()
	local delta = UserInputService:GetMouseDelta()
	currentCamera.CFrame.Rotation = CFrame.Angles(delta.X, delta.Y, 0) -- The Vectors are in a wrong order.
end)

Changed Vector:

RunService:BindToRenderStep("RotateCamera", 50, function()
	local delta = UserInputService:GetMouseDelta()
	currentCamera.CFrame.Rotation = CFrame.Angles(0, delta.X, delta.Y) -- Changed Vector
end)

Check this document about Vectors

Yeah, it does. Except it tilts in the Z axis. I’ll solve that later, thanks.

Additionally, is there any way to make it so a scriptable camera is not affected by Roblox’s core sensitivity settings?