I’m currently trying to make a camera system that’s extremely similar to smash bros ultimate. I’m somewhat new to scripting but I pick up on things fast if explained well.
I like how the camera system moves up until both parts are extremely close or super far away from each other. My goal is to limit the zoom in when both parts are at a specific position. For an example, if 2 parts are 20 studs away from each other, I want the camera to smoothly stop zooming in and hold position until the parts are further away again. https://gyazo.com/54947de532f65022cbd1e45a18567511
Also I’m trying to make it to where if the parts are at a specific position the camera doesn’t track the parts so that they can’t be seen in the camera (this is so that I can experiment with viewport frames) and create a type of death box.
https://gyazo.com/cc96f9f80249564de26dcd197c78a6ba
I tried while loops and if statements within the renderstepped function to change the script zoffset when the distance between the parts are < 16. However it doesn’t work and it bugs out. I’m assuming it has something to do with the renderstep but I’m not exactly sure.
Here’s the code (I placed it in the startergui)
--Services
local rs = game:GetService("RunService")
--Camera
local cam = workspace.CurrentCamera
--CameraOffsets
local xoffset = 0
local yoffset = 2
local zoffset = 5
--Tracking
local tracking = workspace.Tracking
local Player1 = workspace.Tracking.Player1
local Player2 = workspace.Tracking.Player2
--Camera Creation
local camPart = Instance.new("Part")
camPart.Anchored = true
camPart.Name = "CamPart"
camPart.Parent = workspace
camPart.Transparency = 1
camPart.CanCollide = false
--Average position between parts
function calculateAveragePosition()
local total = Vector3.new()
for _, humrootpart in pairs(tracking:GetChildren()) do
total += humrootpart.Position
end
return total / #tracking:GetChildren()
end
--Average Magnitude between parts
function calculateAverageMagnitude()
local total = 0
for _, humrootpart in pairs(tracking:GetChildren()) do
total += (humrootpart.Position - camPart.Position).Magnitude
end
return total / #tracking:GetChildren()
end
--CameraType Change
rs.RenderStepped:Connect(function()
local averagePos = calculateAveragePosition()
local averageMagnitude = calculateAverageMagnitude() + zoffset
rs.Heartbeat:Wait()
cam.CameraType = Enum.CameraType.Scriptable
camPart.Position = averagePos
cam.CFrame = camPart.CFrame * CFrame.new(Vector3.new(xoffset, yoffset, averageMagnitude))
end)
rs.RenderStepped:Connect(function()
rs.Heartbeat:Wait()
print("Player1 and Player2 are " .. math.floor((Player1.Position - Player2.Position).Magnitude) .. " studs away")
end)
I’m not asking for anyone to make the script for me, just asking for tips on how to improve it (if you do improve on the script I appreciate it!).