How do I let the player move the Camera up, down, left and right?

Hello, how do I let the Player move the camera up, down, right, and left?

If anyone could help, thank you.

This is the script I’ve made (StarterCharacterScripts)

local camera = workspace.CurrentCamera
local part = game:GetService("Workspace") 
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = part.Part.CFrame

Are you talking about a freecam-type camera?

No. I’m trying to make the player move the camera up, down, left, and right. I’d like the camera to be in one place

Could you possibly give me an example of what you want?

1 Like

Sure. I apologize if this is a very bad example

image

Oooh! I see. Take a shot with this script


local part = Instance.new("Part")
part.Position = Vector3.new(0, 5, 0)
part.Size = Vector3.new(5, 1, 5)
part.Anchored = true
part.Parent = workspace


local moveX = 0
local moveY = 0
local moveZ = 0
local moveIncrement = 1

local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = CFrame.new(part.Position)


local function moveLeft()
	part.Position = part.Position - Vector3.new(moveIncrement, 0, 0)
end

local function moveRight()
	part.Position = part.Position + Vector3.new(moveIncrement, 0, 0)
end

local function moveUp()
	part.Position = part.Position + Vector3.new(0, moveIncrement, 0)
end

local function moveDown()
	part.Position = part.Position - Vector3.new(0, moveIncrement, 0)
end

local function moveForward()
	part.Position = part.Position + Vector3.new(0, 0, moveIncrement)
end

local function moveBackward()
	part.Position = part.Position - Vector3.new(0, 0, moveIncrement)
end

game:GetService("UserInputService").InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.A then
		moveX = moveX - 1
	elseif input.KeyCode == Enum.KeyCode.D then
		moveX = moveX + 1
	elseif input.KeyCode == Enum.KeyCode.W then
		moveY = moveY + 1
	elseif input.KeyCode == Enum.KeyCode.S then
		moveY = moveY - 1
	end
end)

game:GetService("UserInputService").InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.A then
		moveX = moveX + 1
	elseif input.KeyCode == Enum.KeyCode.D then
		moveX = moveX - 1
	elseif input.KeyCode == Enum.KeyCode.W then
		moveY = moveY - 1
	elseif input.KeyCode == Enum.KeyCode.S then
		moveY = moveY + 1
	end
end)

game:GetService("RunService").RenderStepped:Connect(function(deltaTime)
	local movementVector = Vector3.new(moveX, moveY, moveZ) * moveIncrement
	part.Position = part.Position + movementVector
	camera.CFrame = CFrame.new(part.Position)
end)

2 Likes

Thanks, it worked. However, how would I make it with the mouse too?

Please elaborate. Also if it worked please Solution my post :smiley:

1 Like

I did. What would I do so that if the player moves their mouse up, it goes up and vice versa :slight_smile:

Kind of like freecam, but it’s locked in one position

Personally, I wouldn’t recommend that because it would constantly moving your camera position.

Honestly, I would not know how to go about doing that. You’d use the x and y of the mouse position and set the moveX and moveY correspondingly.

1 Like

Alright. I’ll try to do that :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.