✓ Accepted answer
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:
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local player = game:GetService("Players").LocalPlayer
local camera = game:GetService("Workspace").CurrentCamera
local character = player.Character
local head = character:WaitForChild("Head")
local playerPos = head.Position
local cameraRotation = Vector2.new(0,math.rad(-60))
local cameraOffset = CFrame.new(12, 20, 0)
camera.CameraType = Enum.CameraType.Scriptable
runService.RenderStepped:Connect(function()
if character and playerPos then
playerPos = head.Position
local startCFrame = CFrame.new(playerPos)*CFrame.Angles(0, cameraRotation.X, 0)
local camPos = startCFrame:ToWorldSpace(cameraOffset)
camera.CFrame = CFrame.new(camPos.Position, playerPos)
camera.Focus = CFrame.new(playerPos)
camera.CameraSubject = head
end
end)
function input()
local pressed = userInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton2)
if pressed then
userInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
local rotation = userInputService:GetMouseDelta()
cameraRotation = cameraRotation + rotation * math.rad(.25)
else
userInputService.MouseBehavior = Enum.MouseBehavior.Default
end
end
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.
2 comments
So you don't want the player to lift his head up and down, essentially?
right.