Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I switch to different cameras every so often?

Currently, I am working on making the main menu for a Roblox game that I am trying to develop (It is my first game, which I am using as a practice to learn lua). However, I am having problems with camera manipulation, using youtube tutorials and other questions on this site I managed to make a menu with a single camera angle. I want to change that so about every 60 seconds it switches to a different camera, which is at a different angle and position. Every time the camera changes I also want to make it so that certain lighting values change as well. If anyone can help me with this, that would be very much appreciated and I would be very thankful.

local player = game.Players.LocalPlayer
local character = player.CharacterAdded
local Mouse = player:GetMouse()
local camera = game.Workspace.CurrentCamera

if script.Parent.Parent == game.Players.LocalPlayer:WaitForChild("PlayerGui") then
	game.Workspace.MenuMusic:Play()
end

local defaultCframe = camera.CFrame
local view = 150
--local blur = game.Lighting.Blur
local DOF = game.Lighting.DepthOfField
local ColorCorrection = game.Lighting.ColorCorrection
local SunRays = game.Lighting.SunRays

DOF.FarIntensity = 0.5
DOF.FocusDistance = 12
DOF.InFocusRadius = 10
DOF.NearIntensity = 0
ColorCorrection.Contrast = 0.4
ColorCorrection.Saturation = 0.5
SunRays.Intensity = 0.3


function updateCamera()
	camera.CFrame = game.Workspace.MenuCamera1.CFrame
end

game:GetService("RunService").RenderStepped:Connect(updateCamera)

script.Parent.Frame.Play.MouseButton1Click:Connect(function()
	wait(0.2)
	if player.Team ~= game.Teams.Selecting then
		--blur.Size = 0
		DOF.FarIntensity = 0
		DOF.FocusDistance = 0
		DOF.InFocusRadius = 0
		DOF.NearIntensity = 0
		ColorCorrection.Contrast = 0
		ColorCorrection.Saturation = 0
		game.Workspace.MenuMusic:Stop()
		camera.CameraType = Enum.CameraType.Custom
		script.Parent.Frame:Destroy()
		game.ReplicatedStorage.Respawn:FireServer()
	end
end)

1 answer

I have to go so I'm trying my best

local Players = game:GetService("Players")
local ContextActionService = game:GetService("ContextActionService")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
 
local cameraChase = {left=10, right=10, up=5, down=0}
local cameraDistance = 200
 
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
camera.FieldOfView = 10
 
local player = Players.LocalPlayer
local leftValue, rightValue, initialTouchPos, jumpTouchInput, thumbstickMinPush, cameraPosZ = 0, 0, 0, nil, 15, 0
local humanoid, rootPart
 
local function handleMovement(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		if actionName == "Left" then
			leftValue, rightValue = 1, 0
		elseif actionName == "Right" then
			rightValue, leftValue = 1, 0
		end
	elseif inputState == Enum.UserInputState.End then
		if actionName == "Left" or actionName == "Stop" then
			leftValue = 0
		end
		if actionName == "Right" or actionName == "Stop" then
			rightValue = 0
		end
	end
end
 
local function onMove()
	if humanoid then
		local moveDirection = rightValue - leftValue
		humanoid:Move(Vector3.new(moveDirection, 0, 0), false)
	end
end
 
local function updateCamera()
	if camera.CameraType ~= Enum.CameraType.Scriptable then
		camera.CameraType = Enum.CameraType.Scriptable
	end
 
	if rootPart then
		-- Handle horizontal camera tracking
		if rootPart.Position.X < camera.CFrame.Position.X - cameraChase["left"] then
			camera.CFrame = CFrame.new(Vector3.new(rootPart.Position.X+cameraChase["left"], camera.CFrame.Position.Y, cameraPosZ))
		elseif rootPart.Position.X > camera.CFrame.Position.X + cameraChase["right"] then
			camera.CFrame = CFrame.new(Vector3.new(rootPart.Position.X-cameraChase["right"], camera.CFrame.Position.Y, cameraPosZ))
		end
		-- Handle vertical camera tracking
		if rootPart.Position.Y < camera.CFrame.Position.Y - cameraChase["down"] then
			camera.CFrame = CFrame.new(Vector3.new(camera.CFrame.Position.X, rootPart.Position.Y+cameraChase["down"], cameraPosZ))
		elseif rootPart.Position.Y > camera.CFrame.Position.Y + cameraChase["up"] then
			camera.CFrame = CFrame.new(Vector3.new(camera.CFrame.Position.X, rootPart.Position.Y-cameraChase["up"], cameraPosZ))
		end
	end
end
 
local function handleTouchInput(input, gameProcessed)
	if input.UserInputState == Enum.UserInputState.Begin and gameProcessed == true then
		-- If touch began on jump button, track the touch
		if humanoid.Jump == true then
			jumpTouchInput = input
		-- Else, touch began on thumbstick so set the initial position
		else
			initialTouchPos = input.Position.X
		end
	elseif input.UserInputState == Enum.UserInputState.Change and gameProcessed == true then
		-- For player movement, ignore touches that began on the jump button
		if jumpTouchInput == input then return end
		-- Process movement only when thumbstick is pushed a certain amount
		if input.Position.X < initialTouchPos - thumbstickMinPush then
			handleMovement("Left", Enum.UserInputState.Begin)
		elseif input.Position.X > initialTouchPos + thumbstickMinPush then
			handleMovement("Right", Enum.UserInputState.Begin)
		end
	elseif input.UserInputState == Enum.UserInputState.End then
		-- If touch ended on jump button, stop tracking the touch
		if jumpTouchInput == input then
			jumpTouchInput = nil
		-- Else, touch ended on thumbstick so stop all movement
		else
			handleMovement("Stop", input.UserInputState)
			initialTouchPos = 0
		end
	end
end
 
player.CharacterAdded:Connect(function(character)
 
	humanoid = player.Character:WaitForChild("Humanoid")
	rootPart = player.Character:WaitForChild("HumanoidRootPart")
	-- Set initial camera position
	camera.CFrame = CFrame.new(Vector3.new(rootPart.Position.X, rootPart.Position.Y, rootPart.Position.Z+cameraDistance))
	cameraPosZ = camera.CFrame.Position.Z
	-- If touch is enabled, connect touch handler function
	if UserInputService.TouchEnabled == true then
		UserInputService.TouchStarted:Connect(handleTouchInput)
		UserInputService.TouchMoved:Connect(handleTouchInput)
		UserInputService.TouchEnded:Connect(handleTouchInput)
	end
end)
 
RunService:BindToRenderStep("Input", Enum.RenderPriority.Input.Value, onMove)
RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, updateCamera)
ContextActionService:BindAction("Left", handleMovement, false, "a", Enum.KeyCode.Left)
ContextActionService:BindAction("Right", handleMovement, false, "d", Enum.KeyCode.Right)

2 comments

0

This code probably works, but I am a bit dull.. How would I go about using this..?

0

wait, it's you lmao, from the blur thing

This discussion is preserved as it was. New answers, comments, and votes are closed.