Limiting player camera's X and Z position values

Hello, everyone.

I’m making a top-down view game, and I need to make so the camera doesn’t see stuff it it isn’t supposed to, and by “stuff” I mainly mean the slightly transparent gray blocks in the photo below.

Basically, I need to player to see the map boundaries as camera boundaries. something like this:

I have searched for possible solutions, but no matter what I do it doesn’t as I want it to work.
Here’s the camera code I currently have:

local players = game:GetService("Players")
local runService = game:GetService("RunService")

local player = players.LocalPlayer
local camera = workspace.CurrentCamera

local cameraX
local cameraY
local cameraZ

local cameraPosition
local cameraBoundaries = false
task.wait(3)
repeat
	script:SetAttribute("PlayerToFollow", player.Character.Name)
until player.Character

local function updateCamera()
	local character = workspace:FindFirstChild(script:GetAttribute("PlayerToFollow"))
    if character then
        local root = character:FindFirstChild("HumanoidRootPart")
        if root then
			local rootPosition = root.Position + Vector3.new(0, script:GetAttribute("CameraHeight"), 6) -- CameraHeight must be 40
			
			if rootPosition.X > 31.5 then
				print("X Over 31.5")
				cameraX = 31.5
			elseif rootPosition.X < -31.9 then
				print("X less that -31.9")
				cameraX = -31.9
			else
				print("X")
				cameraX = rootPosition.X
			end
			
			if rootPosition.Z > 53.72 then
				cameraZ = 53.72
			elseif rootPosition.Z < -65 then
				cameraZ = -65
			else
				print("Z")
				cameraZ = rootPosition.Z
			end
			
			
			print(cameraX, cameraZ)
			
			if cameraBoundaries == true then
				cameraPosition = Vector3.new(cameraX, rootPosition.Y, cameraZ - 1)
			else
				cameraPosition = Vector3.new(rootPosition.X, rootPosition.Y, rootPosition.Z - 1)
			end
			--camera.CFrame = CFrame.new(rootPosition, root.Position)
			camera.CFrame = CFrame.lookAt(cameraPosition, rootPosition + Vector3.new(0, -90, 0))
        end
    end
end

runService:BindToRenderStep("90DegreesCamera", Enum.RenderPriority.Camera.Value + 1, updateCamera)

Tell me if you need some additional information or something else entirely.

Thanks for reading!
And helping!
if you do

you can just do something like this to clamp the camera’s position to a boundary, where -10 and 10 is the boundaries to clamp to, and lookDir is the direction to point the camera in

local pos = camera.CFrame.Position

camera.CFrame = CFrame.lookAt(
	Vector3.new(math.clamp(pos.X, -10, 10), pos.Y, math.clamp(pos.Z, -10, 10)),
	pos + lookDir
)

in this case where your boundaries are 31.5 and -31.9, 53.72 and -65, you can put the code like:

local pos = camera.CFrame.Position

camera.CFrame = CFrame.lookAt(
	Vector3.new(math.clamp(pos.X, -31.9, 31.5), pos.Y, math.clamp(pos.Z, -65, 53.72)),
	pos + lookDir
)
2 Likes

I couldn’t get it to work properly, unfortunately. You code did help me get rid of “elseif” Yanderedev style section of my code, but I couldn’t get it to fix what I needed to fix. I think I failed to mention that the camera should always point at the player at 90 degrees. I also failed to mention that at the set border camera does this:

It keeps looking at the player but doesn’t follow him (it should stop looking at the player upon reaching set coordinates).

The problem is I’m not quite sure what I should put at pos + lookDir part at the end. lookDir specifically. Could you explain it to me, please?

1 Like

as @Axominock said, math.clamp should work
you dont even need to use cframe.lookat

camera.CFrame = CFrame.new(Vector3.new(math.clamp(rootPosition.X, -31.9, 31.5), rootPosition.Y + 24, math.clamp(rootPosition.Z, -65, 53.75))) * CFrame.Angles(-math.rad(90), 0, 0)
1 Like

It does work, but:

  1. It flips the Z rotation by 180 degrees (which can be fixed by setting the last thing as CFrame.Angles(-math.rad(90), 0, math.rad(180))
  2. it doesn’t allow me to move for some reason. I’m almost certain it happens because of a custom character

Okay, I was wrong about it being custom character. When I ported everything into a different game it worked perfectly (apart from the border numbers but that’s my problem). I’m gonna look into it. Thanks @Axominock and @dexalernoN for helping me!

1 Like

I have fixed it. Never in a million years would you guess why it didn’t work. The camera script was named "CameraScript" and not "Camera". It didn’t work because it wasn’t named right. I find this very hilarious

1 Like