Static 2D Camera Region Support

I’m trying to replicate the type of camera in Super Mario World or at least most from the series, but I’m just having some jittering issues and I’m also confused on how to change how much the box/camera should reposition by depending on the players velocity.

Here’s two reference videos [1: Code Related] [2: Detailed Explanation]

I’ve already tried using TweenService and :Lerp but I couldn’t get any of it to work.

I know sharing the whole code is not really best practice but I feel it’s best to share for you can get a good understanding. If you can fix this issue, I highly thank you!

local Logic = {}


--\\ Player
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

--\\ Camera
local Cam = workspace.CurrentCamera
local CamPart = game.Workspace.Live.Objects:WaitForChild("CameraVisual")

--\\ Services
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")

--\\ Config
local xoffset = script.Parent:WaitForChild("Xoffset")
local yoffset = script.Parent:WaitForChild("Yoffset")
local zoffset = script.Parent:WaitForChild("Zoffset")
local tracking = workspace.Live:WaitForChild('Players')
local StaticRegion = true

--\\ bounding box parameters (TEST)
local halfSize = Vector3.new(10, 50, 50)

------------------------------------------------------------------------
-- LOCAL FUNCTIONS
------------------------------------------------------------------------

function IsPlayerOutsideBoundingBox(player, boundingBoxCenter)
	local playerPos = player.Character.HumanoidRootPart.Position

	-- Get the min and max of the bounding box
	local boxMin = boundingBoxCenter - halfSize
	local boxMax = boundingBoxCenter + halfSize

	-- if the player is outside the bounding box
	if playerPos.X < boxMin.X or playerPos.X > boxMax.X or
		playerPos.Y < boxMin.Y or playerPos.Y > boxMax.Y or
		playerPos.Z < boxMin.Z or playerPos.Z > boxMax.Z then
		return true
	else
		return false
	end
end

function CreateBoundingBoxVisual(boundingBoxCenter, Reposition)
	-- Create a part twin
	if Reposition then game.Workspace:FindFirstChild("BoundingBoxVisual").Position = Reposition return end
	
	if game.Workspace:FindFirstChild("BoundingBoxVisual") then return end
	
	local boxPart = Instance.new("Part")
	boxPart.Name = "BoundingBoxVisual"
	boxPart.Size = halfSize * 2
	boxPart.Position = boundingBoxCenter
	boxPart.Anchored = true
	boxPart.CanCollide = false
	boxPart.Transparency = 0
	boxPart.Material = Enum.Material.ForceField
	boxPart.CastShadow = false
	boxPart.Color = Color3.fromRGB(0, 255, 0)
	boxPart.Parent = workspace
end

------------------------------------------------------------------------
-- GLOBAL FUNCTIONS
------------------------------------------------------------------------

function Logic.Convert2D()
	if Cam.CameraType ~= Enum.CameraType.Scriptable then Cam.CameraType = Enum.CameraType.Scriptable end
	
	-- Init stuff
	CreateBoundingBoxVisual(HumanoidRootPart.Position)
	CamPart.Position = Vector3.new(HumanoidRootPart.Position.X, 5, HumanoidRootPart.Position.Z + 25)
	
	local Connection = RunService.RenderStepped:Connect(function()
		if IsPlayerOutsideBoundingBox(Player, CamPart.Position) then
			local PlayerValue = 0.025 -- This should change to adapt to the players speed
			
			CreateBoundingBoxVisual(nil, Vector3.new(CamPart.Position.X + PlayerValue * HumanoidRootPart.AssemblyLinearVelocity.X, 5, CamPart.Position.Z))
			CamPart.Position = Vector3.new(CamPart.Position.X + PlayerValue * HumanoidRootPart.AssemblyLinearVelocity.X, 5, CamPart.Position.Z)
		end
		
		-- just setting the camera based on the part
		Cam.CFrame = CamPart.CFrame * CFrame.new(Vector3.new(xoffset.Value, yoffset.Value, zoffset.Value))
	end)
	
	return Connection
end


return Logic

The code above is very messy, don’t mind it lol

How about moving camera expected frames based on the velocity of player on every frame?
(use RunService.HeartBeat?)

I’ll try and see if that works!

use BindToRenderStep instead of Heartbeat

If there are no complex moving, you can decide camera move based on player input and steady acceleration. (Just like 2D mario series, not Mario 64)

I would use BindToRenderStep but it doesn’t return DeltaTime, which fixes another issue of mine that I didn’t originally include in my post. Thanks though!

I’ll try that instead. although my game will involve complex movement, I’m sure this would be a better approach. The issue most likely has to do with the IsPlayerOutsideBoundingBox function I have. Just like how Part.Touched works weirdly

I leave some hints here.
When player input specific key, camera speed is the walkspeed of character.
When player is falling, camera speed increases per acceleration of gravity.
Like this, you can calculate camera speed. (you need to think about player’s Position apart)

Good Luck!

BindToRenderStep does pass delta time

I could be doing something wrong but in the docs it doesn’t say BindToRenderStep returns anything

Thanks! I appreciate it the advice!

It does. If you scroll down a bit its under “Custom Function and Delta Time”. They also use it in their example.
Try just running

local rs = game:GetService("RunService");
rs:BindToRenderStep("Name", 1, print);
task.wait(0.1);
rs:UnbindFromRenderStep("Name");

in command bar.

Oh shoot, You’re right, I must have done it wrong, thank you for the advice, it does make it slightly smoother but still jittering. It’s most likely how I handle the detection or something else.

Thank you once again!