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