wait() local Player = game.Players.LocalPlayer local Camera = game.Workspace.CurrentCamera Camera.CameraSubject = script.Parent.Head Camera.CameraType = "Attach"
I like that using cameratype:"attach" works well by locking one axis, but how can I just have the camera rotate on the y axis?
You can't use the "Attach" type, you have to use "Scriptable". Here's the code I'm using for the game I'm currently working on. It's probably not quite what you're doing, but you should be able to adapt it easily enough:
-- Player camera script -- By TerminusEstKuldin -- Services local userInputService = game:GetService("UserInputService") local runService = game:GetService("RunService") -- Player & Camera local player = game:GetService("Players").LocalPlayer local camera = game:GetService("Workspace").CurrentCamera -- Models local character = player.Character local head = character:WaitForChild("Head") local playerPos = head.Position -- Camera values local cameraRotation = Vector2.new(0,math.rad(-60)) local cameraOffset = CFrame.new(12, 20, 0) -- Set camera type camera.CameraType = Enum.CameraType.Scriptable -- Refresh camera every frame runService.RenderStepped:Connect(function() if character and playerPos then -- Start at player position playerPos = head.Position -- Rotate CFrame by cameraRotation value local startCFrame = CFrame.new(playerPos)*CFrame.Angles(0, cameraRotation.X, 0) -- Offset CFrame by defined offset local camPos = startCFrame:ToWorldSpace(cameraOffset) -- Assign attributes to camera camera.CFrame = CFrame.new(camPos.Position, playerPos) camera.Focus = CFrame.new(playerPos) camera.CameraSubject = head end end) -- Function to check user input to pan camera function input() -- Check if right mouse button is pressed local pressed = userInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton2) if pressed then -- Lock mouse on screen userInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition -- Adjust cameraRotation by the value of mouse movement local rotation = userInputService:GetMouseDelta() cameraRotation = cameraRotation + rotation * math.rad(.25) else -- Unlock mouse userInputService.MouseBehavior = Enum.MouseBehavior.Default end end -- Listen for user input userInputService.InputBegan:Connect(input) userInputService.InputChanged:Connect(input) userInputService.InputEnded:Connect(input)
So what this does is first script the camera to be updated every frame, but only adjusts it by the X value of the camera rotation, not the Y value. It then sets up the InputService to listen for the right mouse button to be pressed and then records the amount the mouse moves to be turned into an equivalent camera rotation. Depending on where you want your camera to view from, you just have to change the CFrame of your camera.