Hello, I made a custom camera script and need help adding a feature to it. Right now all it does to automatically move around depending on where the character is. But I also want it to move manually using a mouse, touchscreen, or an Xbox controller like the regular camera does. How can I achieve this?
Script: (Goes in StarterGUI)
local sp = script.Parent
local player = game.Players.LocalPlayer
local runService = game:GetService('RunService')
local cam = game.Workspace.CurrentCamera
local p = game.Workspace:FindFirstChild('CameraPart') or Instance.new('Part')
p.Name = 'CameraPart'
p.TopSurface = 'Smooth'
p.BottomSurface = 'Smooth'
p.FrontSurface = 'Hinge'
p.Anchored = true
p.CanCollide = false
p.Parent = game.Workspace
local stickyness = .02
local desiredHeight = 12
local desiredDist = 25
local maxDist = 40
local minDist = 20
local function swait(a)
if a and a>.0333 then
wait(a)
else
runService.RenderStepped:wait()
end
end
while true do
swait()
local character = player.Character
if character and character.Parent == game.Workspace then
local target = character:FindFirstChild('Torso')
if target then
cam.CameraType = 'Scriptable'
local vectorAway = ((cam.CoordinateFrame.p - target.Position) * Vector3.new(1,0,1)).unit
if vectorAway.x ~= vectorAway.x then
vectorAway = Vector3.new(1,0,0)
end
local desiredPos = target.Position + vectorAway * desiredDist + Vector3.new(0,desiredHeight,0)
local resultPos = desiredPos * stickyness + cam.CoordinateFrame.p * (1-stickyness)
local resultVec = resultPos - target.Position
local resultUnitVec = (resultVec).unit
if resultUnitVec.x ~= resultUnitVec.x then
resultUnitVec = Vector3.new(1,0,0)
end
local gotClamped = false
if resultVec.magnitude > maxDist then
resultVec = resultUnitVec * maxDist
gotClamped = true
elseif resultVec.magnitude < minDist then
resultVec = resultUnitVec * minDist
gotClamped = true
end
if gotClamped then
cam.CoordinateFrame = CFrame.new(target.Position+resultVec,target.Position)
else
cam.CoordinateFrame = CFrame.new(resultPos,target.Position)
end
end
end
end