Camera Movement With Mouse

Hi there!

I’m creating a custom set of controls my new game where there isn’t any Roblox characters. I’ve got everything to work really well, including a sort of first person mode where you can use a cursor to shoot instead of mouse placement.

However, the issue I have is that I want the camera to move when the mouse moves sort of like most AAA third-person games. The same effect can be achieved easily but holding down right-click and moving the mouse, however I’d like this to just be the default.

Any help would be appreciated,
-Tom :slight_smile:

1 Like

Is this what you’re looking for

Over-the-Shoulder

A basic over-the-shoulder camera, commonly found in third-person shooter games, can be achieved with the following script. This camera stays locked behind the character’s back and players use the mouse to turn (not directional input).

1. local Players = game:GetService("Players")
2. local ContextActionService = game:GetService("ContextActionService")
3. local UserInputService = game:GetService("UserInputService")
4. local RunService = game:GetService("RunService")
 * local camera = workspace.CurrentCamera
5. local cameraOffset = Vector3.new(2, 2, 8)
6. local player = Players.LocalPlayer
 * player.CharacterAdded:Connect(function(character)
 * local humanoid = character:WaitForChild("Humanoid")
7. local rootPart = character:WaitForChild("HumanoidRootPart")
8. humanoid.AutoRotate = false
 * local cameraAngleX = 0
9. local cameraAngleY = 0
 * local function playerInput(actionName, inputState, inputObject)
10. -- Calculate camera/player rotation on input change
11. if inputState == Enum.UserInputState.Change then
12. cameraAngleX = cameraAngleX - inputObject.Delta.X
13. -- Reduce vertical mouse/touch sensitivity and clamp vertical axis
14. cameraAngleY = math.clamp(cameraAngleY-inputObject.Delta.Y*0.4, -75, 75)
15. -- Rotate root part CFrame by X delta
16. rootPart.CFrame = rootPart.CFrame * CFrame.Angles(0, math.rad(-inputObject.Delta.X), 0)
17. end
18. end
19. ContextActionService:BindAction("PlayerInput", playerInput, false, Enum.UserInputType.MouseMovement, Enum.UserInputType.Touch)
 * RunService.RenderStepped:Connect(function()
20. if camera.CameraType ~= Enum.CameraType.Scriptable then
21. camera.CameraType = Enum.CameraType.Scriptable
22. end
23. local startCFrame = CFrame.new((rootPart.CFrame.Position)) * CFrame.Angles(0, math.rad(cameraAngleX), 0) * CFrame.Angles(math.rad(cameraAngleY), 0, 0)
24. local cameraCFrame = startCFrame:ToWorldSpace(CFrame.new(cameraOffset.X, cameraOffset.Y, cameraOffset.Z))
25. local cameraFocus = startCFrame:ToWorldSpace(CFrame.new(cameraOffset.X, cameraOffset.Y, -10000))
26. camera.CFrame = CFrame.new(cameraCFrame.Position, cameraFocus.Position)
27. end)
28. end)
 * local function focusControl(actionName, inputState, inputObject)
29. -- Lock and hide mouse icon on input began
30. if inputState == Enum.UserInputState.Begin then
31. UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
32. UserInputService.MouseIconEnabled = false
33. ContextActionService:UnbindAction("FocusControl", focusControl, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch, Enum.UserInputType.Focus)
34. end
35. end
36. ContextActionService:BindAction("FocusControl", focusControl, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch, Enum.UserInputType.Focus)

Note the camera offset values on line 7 — these can be adjusted to change the camera’s relative position to the character, for example to place it over the character’s left shoulder ( -2, 2, 8 ) or pull the camera further away from the player ( 2, 2, 15 ).

local uis = game:GetService("UserInputerService")
local rs = game:GetService("RunService")


local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local root = character:WaitForChild("HumanoidRootPart")
local camera = workspace.CurrentCamera


local rotX = 0
local rotY = 0
local zoom = 10
local offsetX = 0
local delta
local sensitivity = 0.1


camera.CameraType = Enum.CameraType.Scriptable
uis.MouseBehavior = Enum.MouseBehavior.LockCenter


uis.InputChanged:Connect(function(i,g)
	if(not g)then
		if(i.UserInputType == Enum.UserInputType.MouseMovement)then
			uis.MouseBehavior = Enum.MouseBehavior.LockCenter
			delta = uis:GetMouseDelta()
			rotX = (rotX == 360 or rotX == -360) and 0 or math.clamp(rotX+-sensitivity, -360, 360)
			rotY = math.clamp(rotY +- delta.Y * sensitivity, 1, 90 - (zoom+2))
		end
	end
end)



rs.RenderStepped:Connect(function()
	camera.CFrame = ((CFrame.new(root.Position) --position
		* CFrame.Angles(0, math.rad(rotX), 0)) --rotate X
		* CFrame.Angles(math.rad(rotY), 0, 0)) --rotate Y
		* CFrame.new(offsetX,zoom,zoom/2) --offset
	
	--make the camera face the subject
	camera.CFrame = CFrame.new(camera.CFrame.Position, root.Position)
end)

i think something like this would be okay and then use joystick/mouse deltas to update rotX and rotY you can also use other buttons to update zoom and horizontal offset.

2 Likes