PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Custom Camera Control: Code Sample 2

Custom Camera Control: Code Sample 2

Oct 01 2018, 3:13 AM PST

local cameraHeight = 12
local cameraZOffset = 20
local cameraXChase = 10
local cameraSpeed = .25

local camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local RunService = game:GetService('RunService')

local function setupCamera()
	camera.CFrame = CFrame.new(Vector3.new(0,cameraHeight,cameraZOffset),
										Vector3.new(0,cameraHeight,0))
end
setupCamera()
player.CharacterAdded:Connect(setupCamera)

local function onUpdate()
	if player.Character and player.Character:FindFirstChild('Torso') then
		local playerX = player.Character.Torso.Position.X
		local cameraX = camera.CFrame.p.X
		
		if cameraX - cameraXChase < playerX then
			camera.CFrame = camera.CFrame + Vector3.new(cameraSpeed, 0, 0)
		end
	end
end

RunService:BindToRenderStep('Camera', Enum.RenderPriority.Camera.Value, onUpdate)