# How can I limit this CFrame?

Hello, I’m trying to create a security camera where you’re able to move up, down, left, or right. But I want the player to be limited where they can move, so like if it reaches a certain cframe it’ll stop, but you’re able to move in the other direction.

``````local run = game:GetService("RunService")

function MoveCamera()
local MoveSpeed = 5

local CurrentCamera = workspace.CurrentCamera

local CameraPos

local CanMoveLeft = true
local CanMoveRight = true
local CanMoveTop = true
local CanMoveDown = true

if UIS:IsKeyDown(Enum.KeyCode.W) or UIS:IsKeyDown(Enum.KeyCode.Up) then
if CanMoveTop then
end
elseif UIS:IsKeyDown(Enum.KeyCode.S) or UIS:IsKeyDown(Enum.KeyCode.Down) then
if CanMoveDown then
end
elseif UIS:IsKeyDown(Enum.KeyCode.D) or UIS:IsKeyDown(Enum.KeyCode.Right) then
if CanMoveRight then
end
elseif UIS:IsKeyDown(Enum.KeyCode.A) or UIS:IsKeyDown(Enum.KeyCode.Left) then
if CanMoveLeft then
end
end

if CameraPos then
CurrentCamera.CFrame = CurrentCamera.CFrame * CameraPos
end
end

run.Heartbeat:Connect(MoveCamera)
``````

You can use the math.clamp() function to set a min and max value.

1 Like

I’ve seen that, but I’m confused how I would implement that into my code?

1 Like

wait i did the position not the roation xd

You’d have to clamp the X, Y and Z values seperately unless you want them to have the same limit. You can achieve that like so:

``````local pos = CurrentCamera.Position
local X, Y, Z = math.clamp(pos.X, -50, 50), math.clamp(pos.Y, -80, 80), math.clamp(pos.Z, -100, 100)
CurrentCamera.Position = Vector3.new(X, Y, Z)
CurrentCamera.CFrame *= CameraPos
``````
1 Like

@ElectrifyThunder Can you figure it out for the rotation using the concept I gave for the position accidently?

you could do something like this

``````local run = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local minVec = Vector2.new(-1,-1)
local maxVec = Vector2.new(1,1)
local cVec = Vector2.zero

local moveVecs = {
[Enum.KeyCode.W] = Vector2.new(MoveSpeed,0),
[Enum.KeyCode.Up] = Vector2.new(MoveSpeed,0),
[Enum.KeyCode.S] = Vector2.new(-MoveSpeed,0),
[Enum.KeyCode.Down] = Vector2.new(-MoveSpeed,0),
[Enum.KeyCode.A] = Vector2.new(0,MoveSpeed),
[Enum.KeyCode.Left] = Vector2.new(0,MoveSpeed),
[Enum.KeyCode.D] = Vector2.new(0,-MoveSpeed),
[Enum.KeyCode.Right] = Vector2.new(0,-MoveSpeed)
}

function MoveCamera()
local moveVec = Vector2.zero

for key, vec in pairs(moveVecs) do
if UIS:IsKeyDown(key) then
moveVec += vec
end
end

if cVec.X+moveVec.X > maxVec.X then
moveVec = Vector2.new(maxVec.X-cVec.X,moveVec.Y)
end
if cVec.Y+moveVec.Y > maxVec.Y then
moveVec = Vector2.new(moveVec.X,maxVec.Y-cVec.Y)
end
if cVec.X+moveVec.X < minVec.X then
moveVec = Vector2.new(minVec.X-cVec.X,moveVec.Y)
end
if cVec.Y+moveVec.Y < minVec.Y then
moveVec = Vector2.new(moveVec.X,minVec.Y-cVec.Y)
end
cVec += moveVec
workspace.CurrentCamera.CFrame *= CFrame.Angles(moveVec.X,moveVec.Y,0)
end

run.Heartbeat:Connect(MoveCamera)

``````
1 Like

Thanks! I’ve been struggling on this for a bit.