[SOLVED] Free camera system not working as intended

I am very tired despite all the coffee I drank so this might have some obvious mistakes.

For now, this just detects A and W keys.

local Camera = game.Workspace.CurrentCamera
local Lplr = game.Players.LocalPlayer
local InS = game:GetService("UserInputService")

local r = math.rad

local CameraReferancePart = Instance.new("Part")


local StartCF = CFrame.new(0,10,0)
local StartR = CFrame.Angles(r(-90),0,0)

local kW = Enum.KeyCode.W
local kD = Enum.KeyCode.D
local kA = Enum.KeyCode.A
local kS = Enum.KeyCode.S
local kQ = Enum.KeyCode.Q
local kE = Enum.KeyCode.E





InS.InputBegan:Connect(function(Input,IsType)
	if IsType == false then
		if Input.UserInputType == Enum.UserInputType.Keyboard then
			if Input.KeyCode == kW then
				repeat
					task.wait()
					Camera.CFrame+= Vector3.new(0,0,1)
					local K = InS.InputChanged:Wait(1)
				until K.KeyCode == kW

			elseif Input.KeyCode == kA then
				repeat
					task.wait()
					Camera.CFrame+= Vector3.new(0,1,0)
				until InS.InputEnded:Wait().KeyCode == kA
			end
		end
	end
end)

while task.wait() do
	Camera.CameraSubject = CameraReferancePart
	Camera.CameraType = Enum.CameraType.Scriptable
end

Problem is probably the hold detection

W will continuesly move in every input.

Basically it moves W every time a key is held, not if the key W is held though…

Can you describe in a little more detail exactly what the script is currently doing and what it’s supposed to do? I just inserted it into studio and it seems completely broken.

It is meant to serve as a free camera.

To go around a map

As long as W has been pressed, it will fire the function that W is meant to have every time any input is added

local Run = game:GetService("RunService")
local UserInput = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Camera = workspace.CurrentCamera
Camera.CameraType = Enum.CameraType.Scriptable
Camera.CFrame = CFrame.new(0, 0, 0)

local Keys = {Enum.KeyCode.W, Enum.KeyCode.A, Enum.KeyCode.S, Enum.KeyCode.D, Enum.KeyCode.Q, Enum.KeyCode.E}
local KeyToggles = {[Enum.KeyCode.W] = false, [Enum.KeyCode.A] = false, [Enum.KeyCode.S] = false, [Enum.KeyCode.D] = false, [Enum.KeyCode.Q] = false, [Enum.KeyCode.E] = false}

local function InputStarted(Input, Processed)
	if Processed then
		return
	end
	
	for Key, Boolean in pairs(KeyToggles) do
		if Input.KeyCode == Key then
			KeyToggles[Input.KeyCode] = true
			while KeyToggles[Input.KeyCode] do
				task.wait()
				if Key == Keys[1] then
					Camera.CFrame *= CFrame.new(0, 0, -0.5)
				elseif Key == Keys[2] then
					Camera.CFrame *= CFrame.new(-0.5, 0, 0)
				elseif Key == Keys[3] then
					Camera.CFrame *= CFrame.new(0, 0, 0.5)
				elseif Key == Keys[4] then
					Camera.CFrame *= CFrame.new(0.5, 0, 0)
				elseif Key == Keys[5] then
					Camera.CFrame *= CFrame.Angles(0, math.rad(1), 0)
				elseif Key == Keys[6] then
					Camera.CFrame *= CFrame.Angles(0, math.rad(-1), 0)
				end
			end
		end
	end
end

local function InputEnded(Input, Processed)
	if Processed then
		return
	end
	
	for Key, Boolean in pairs(KeyToggles) do
		if Input.KeyCode == Key then
			KeyToggles[Input.KeyCode] = false
		end
	end
end

UserInput.InputBegan:Connect(InputStarted)
UserInput.InputEnded:Connect(InputEnded)
1 Like

I put some zoom in and out movements with the wheel forwards and backwards and added the camera subject so it would actually work. But apart from that, good job!

I was going to ask if you’d like some improvements adding, why do you need a camera subject? I didn’t.

local Run = game:GetService("RunService")
local UserInput = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HRP = Character:WaitForChild("HumanoidRootPart")
local Mouse = Player:GetMouse()
local Camera = workspace.CurrentCamera
Camera.CameraType = Enum.CameraType.Scriptable
Camera.CFrame = CFrame.new(0, 0, 0)

local Keys = {Enum.KeyCode.W, Enum.KeyCode.A, Enum.KeyCode.S, Enum.KeyCode.D, Enum.KeyCode.Q, Enum.KeyCode.E}
local KeyToggles = {[Enum.KeyCode.W] = false, [Enum.KeyCode.A] = false, [Enum.KeyCode.S] = false, [Enum.KeyCode.D] = false, [Enum.KeyCode.Q] = false, [Enum.KeyCode.E] = false}
local RightMouseDown = false

local function InputStarted(Input, Processed)
	if Processed then
		return
	end
	
	for Key, Boolean in pairs(KeyToggles) do
		if Input.KeyCode == Key then
			KeyToggles[Input.KeyCode] = true
			while KeyToggles[Input.KeyCode] do
				task.wait()
				if Key == Keys[1] then
					Camera.CFrame *= CFrame.new(0, 0, -0.5)
				elseif Key == Keys[2] then
					Camera.CFrame *= CFrame.new(-0.5, 0, 0)
				elseif Key == Keys[3] then
					Camera.CFrame *= CFrame.new(0, 0, 0.5)
				elseif Key == Keys[4] then
					Camera.CFrame *= CFrame.new(0.5, 0, 0)
				elseif Key == Keys[5] then
					Camera.CFrame *= CFrame.Angles(0, math.rad(1), 0)
				elseif Key == Keys[6] then
					Camera.CFrame *= CFrame.Angles(0, math.rad(-1), 0)
				end
			end
		end
	end
end

local function InputEnded(Input, Processed)
	if Processed then
		return
	end
	
	for Key, Boolean in pairs(KeyToggles) do
		if Input.KeyCode == Key then
			KeyToggles[Input.KeyCode] = false
		end
	end
end

local function MouseScrollUp()
	Camera.CFrame *= CFrame.new(0, 0, -5)
end

local function MouseScrollDown()
	Camera.CFrame *= CFrame.new(0, 0, 5)
end

local function MouseLeftDown()
	Camera.CFrame = HRP.CFrame
end

local function MouseRightDown()
	Camera.CFrame = Mouse.Hit
end

UserInput.InputBegan:Connect(InputStarted)
UserInput.InputEnded:Connect(InputEnded)
Mouse.WheelForward:Connect(MouseScrollUp)
Mouse.WheelBackward:Connect(MouseScrollDown)
Mouse.Button1Down:Connect(MouseLeftDown)
Mouse.Button2Down:Connect(MouseRightDown)

Slight changes, scrolling adding, left click resets the camera back to the player’s character’s location and right click teleports the camera to the click location.

1 Like

I’ll get rid of the clicks but everything else is perfect :happy1: