Creating a free roam camera

game:GetService('UserInputService').InputChanged:connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
		if var.EditCam then
			if var.Editing then
				if Button2Down then
					game:GetService('UserInputService').MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
					camera.CFrame = camera.CFrame * CFrame.Angles(0, math.rad(inputObject.Delta.X/25), 0)
				else
					game:GetService('UserInputService').MouseBehavior = Enum.MouseBehavior.Default
				end
			end
		end
	end
end)

Currently using this, when a player right clicks I want them the camera to rotate on the pivot, but it rotates awkwardly.



As you can see it kind of rotates at a weird angle. I’ve managed to rotate the camera in the past around, like this:

    CFrame.new(target.Position) * CFrame.Angles(0, angle, 0)

with angle being updated every tick to rotate around a part. But here I am not trying to rottate around a part, I want the camera to just rotate as the player clicks. The effect I’m trying to go for is like how the camera works within studio when you right click and drag the mouse around

2 Likes

Still no reply :confused:

game:GetService('UserInputService').InputChanged:connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
		if var.EditCam then
			if var.Editing then
				if Button2Down then
					game:GetService('UserInputService').MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
					camera.CFrame = camera.CFrame * CFrame.Angles(math.rad(-inputObject.Delta.y/25), math.rad(-inputObject.Delta.x/25),0)
				else
					game:GetService('UserInputService').MouseBehavior = Enum.MouseBehavior.Default
				end
			end
		end
	end
end)

mouse.Button2Down:connect(function() Button2Down = true end)
mouse.Button2Up:connect(function() Button2Down = false end) 

This is what I have. Camera is still rotating on weird angles with the right click. I’ve copied this other from another place and it works perfectly there, but when I put it in my place it does all kinds of crazy things.

It has to do with this line though:

camera.CFrame = camera.CFrame * CFrame.Angles(math.rad(-inputObject.Delta.y/25), math.rad(-inputObject.Delta.x/25),0)

As when I comment that line out in both places, right click wont do anything, so this line is affecting how the camera rotates, but I don’t know how to get it to rotate properly how I want it to.

Pan? I believe is the word I am looking for. trying to make the camera pan, and so it should function as pressing the right click normally does when the camera is focused on the player

To rotate the camera around the part’s position, I would use some sort of local space transformation.

local rotationAngle = math.rad(90)
local CameraInPartSpace = Camera.CFrame - yourPart.CFrame -- This is the relative CFrame of the camera to the target
local RotatedPartSpace = yourPart.CFrame * CFrame.fromAxisAngle(Vector3.new(0, 1, 0), rotationAngle) -- This creates a cframe with the same position as the part, but rotated around the y-axis.

local adjustedCameraCFrame = CameraInPartSpace:toWorldSpace(RotatedPartSpace)

The basic idea of this is that you are finding the relative position of the camera to the part, rotating the part, and then using the camera’s relative position to find a new world position which is at the same distance from the part, but at a different position and rotation.