Hello, I am making an isometric game and it has a camera system where you can zoom in and out to minimum and max limits. I want to make it so when you are zooming in/out, it shows how much percentage you are zoomed in compared to the default amount you start on. For example, the default amount’s percentage is 100%, when you are fully zoomed in, the percentage is 200%, when fully zoomed out, the percentage is 0%. Any help would be appreciated, thank you.
Percentage zoomed in = ((Current Zoom - Reference Zoom)/Reference Zoom)*100
local Camera = game.Workspace.CurrentCamera -- Assuming the camera you want to track is in the Workspace
local DefaultZoom = 10 -- Adjust this value to your default zoom level
local MinZoom = 5 -- Adjust this value to your minimum zoom level
local MaxZoom = 20 -- Adjust this value to your maximum zoom level
local function CalculatePercentageZoomedIn(currentZoom, referenceZoom)
return ((currentZoom - referenceZoom) / referenceZoom) * 100
end
local function UpdateUI()
local CurrentZoom = Camera.Focus.Position.magnitude -- Get the current zoom level (distance from camera to its focus point)
local DefaultPercentage = CalculatePercentageZoomedIn(CurrentZoom, DefaultZoom)
local MinPercentage = CalculatePercentageZoomedIn(CurrentZoom, MinZoom)
local MaxPercentage = CalculatePercentageZoomedIn(CurrentZoom, MaxZoom)
-- Update UI elements with the calculated percentages
-- Example:
-- script.Parent.DefaultZoomText.Text = string.format("%.2f%%", DefaultPercentage)
-- script.Parent.MinZoomText.Text = string.format("%.2f%%", MinPercentage)
-- script.Parent.MaxZoomText.Text = string.format("%.2f%%", MaxPercentage)
end
-- Connect the UpdateUI function to run when the camera zoom changes
Camera:GetPropertyChangedSignal("Focus") :Connect(UpdateUI)
-- Call UpdateUI initially to update the UI with the initial zoom level
UpdateUI()
this should work ig
To do this you would just get the current camera ZoomDistance
and divide it by the minimum and maximum ZoomDistance
of the camera.
-- Camera Zoom Percentage Script
-- Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
-- Variables
local cam = workspace.CurrentCamera
local player = Players.LocalPlayer
local max_zoom = player.CameraMaxZoomDistance
local min_zoom = player.CameraMinZoomDistance
-- Functions
local function GetZoom()
return (cam.CFrame.Position - cam.Focus.Position).Magnitude
end
-- Init
RunService.RenderStepped:Connect(function()
local current_zoom = GetZoom()
local percent = math.round(
(current_zoom / max_zoom) * 100
)
end)
The above script will get the percent relative to the maximum zoom level of the camera. I am still unsure what you mean by “default amount”. When you are fully zoomed out wouldn’t you be at 100% of the zoom, not 200%?
Exactly, but if he talks about the distance when you spawn? Im not sure how you’d get that
Well the distance when you spawn would just be the GetZoom()
function I have defined, only it would just be called at the beginning.
GetZoom()
just returns the current zoom amount in studs, which is what the game uses to determine maximum and minimum zooming.
EDIT: The part that I don’t understand is why they would have the default zoom amount as 100%, as that would imply (at least from a user standpoint) that you are fully zoomed out.
maybe instead of doing it on 100% ((current_zoom / max_zoom) * 100), we just do (current_zoom / max_zoom) * 200? Its confusing why he wants 200%, maybe its a typo?
I mainly want to start it on 100% as I’m taking inspiration from a game that does it that way, but I’m open to trying it other ways. https://gyazo.com/c744b4d2694b506e540cb46f824060ad
See how in this clip it starts out at 100%, as it zooms in it goes higher than that, and then when you zoom out it goes lower.
That would work, however, the default view still wouldn’t be displayed as 100%. It would only be about 20%, based on what the max and min zoom properties are set to.
To get the default amount’s percentage to be 100%, you would have to alter the script a bit.
Define a default_zoom
variable in the “Variables” section of the script:
local default_zoom = 12.5 -- Roblox default camera zoom amount
Create a new function that will update default_zoom
if min_zoom
is greater than default_zoom
or max_zoom
is less than default_zoom
:
local function UpdateDefaultZoom()
if min_zoom > default_zoom then
default_zoom = min_zoom
elseif max_zoom < default_zoom then
default_zoom = max_zoom
end
end
Update the RunService
function and call the UpdateDefaultZoom()
function in the “Init” section of the script:
UpdateDefaultZoom()
RunService.RenderStepped:Connect(function()
local current_zoom = GetZoom()
local percent
if current_zoom >= default_zoom then
percent = math.round(((max_zoom / current_zoom) / 10) * 100)
else
percent = math.round(((min_zoom / current_zoom) * 100) + 100)
end
-- Update UI with percent variable
print(percent)
end)
The percent
variable will now display a higher value when zoomed in and a lower value when zoomed out.
When Roblox positions the camera, the default camera zoom amount is 12.5. If the CameraMinZoomDistance
property is greater than 12.5 then it sets it to the CameraMinZoomDistance
property. If the CameraMaxZoomDistance
is less than 12.5, then it sets it to the CameraMaxZoomDistance
property. The UpdateDefaultZoom()
function manages this for you.
Hopefully all of this answers any question that you have, @TheSwipedSwiper
EDIT:
Full script
-- Camera Zoom Percentage Script
-- Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
-- Variables
local cam = workspace.CurrentCamera
local player = Players.LocalPlayer
local max_zoom = player.CameraMaxZoomDistance
local min_zoom = player.CameraMinZoomDistance
local default_zoom = 12.5 -- Roblox default camera zoom amount
-- Functions
local function UpdateDefaultZoom()
if min_zoom > default_zoom then
default_zoom = min_zoom
elseif max_zoom < default_zoom then
default_zoom = max_zoom
end
end
local function GetZoom()
return (cam.CFrame.Position - cam.Focus.Position).Magnitude
end
-- Init
UpdateDefaultZoom()
RunService.RenderStepped:Connect(function()
local current_zoom = GetZoom()
local percent
if current_zoom >= default_zoom then
percent = math.round(((max_zoom / current_zoom) / 10) * 100)
else
percent = math.round(((min_zoom / current_zoom) * 100) + 100)
end
-- Update UI with percent variable
print(percent)
end)
EDIT EDIT: I realized I forgot to explain the math behind the percentages.
Essentially, if current_zoom
is greater than default_zoom
(you are zooming away from your character) it takes the max_zoom
and divides it by the current_zoom
. This will result in a fraction where the numerator is larger than the denominator. To account for this, we divide by 10, pushing the decimal place of the fraction over by 1, resulting in a value less than 1. We then multiply by 100, giving us the percent.
If current_zoom
is less than default_zoom
(you are zooming toward your character) it takes the min_zoom
and divides it by the current_zoom
. This returns a value less than 1. We then multiply by 100 to get the percent of the value, and then add 100 to account for the default percent.
This isn’t spot on and it’s not showing a percentage of the zoom. But, it is short and sweet. Maybe you can modify it to fit your needs. It’s not off by much (head movement is throwing it off by a bit). This will show the current actual zoom level… It is based off what the camera is focused on, the head.
A totally different approach…
--local script
local rns=game:GetService("RunService")
local cam=workspace.CurrentCamera
local player=game:GetService("Players").LocalPlayer
local head=player.Character:WaitForChild("Head")
rns.RenderStepped:Connect(function()
local camZ=cam.CFrame.LookVector.Z
local headZ=(head.Position-cam.CFrame.Position).Magnitude
local magnitude=math.abs(headZ/camZ)
print('zoom level: '.. magnitude)
end)
I was thinking that this could work, however, OP is looking for a way to display the percentage higher when zooming in, lower when zooming out, and 100% at the default position (12.5 studs).
Refer to my explanation below for how this works:
I know, I got distracted. But, it still kind of relates. Thinking you guys could take it from here. If it wasn’t for that head movement it would get the number perfectly and you could figure percentage from that. Maybe he would be happy with percentage-ish.
Edit: Also this is off by a few hundredths (very very slightly off). If that was rounded off, I’m sure that small of an amount really isn’t going to matter.
Your function gives this kind of curve:
I believe this is the kind of curve OP wants:
-- linear interpolation function
local function lerp(a: number, b: number, t: number): number
return a + (b - a) * t
end
local function getZoomPercent(currentZoomDist: number): number
local nonRoundedZoomDist: number
if currentZoomDist < defaultZoomDist then
nonRoundedZoomDist = lerp(200, 100, (currentZoomDist - minZoomDist) / (defaultZoomDist - minZoomDist))
else
nonRoundedZoomDist = lerp(100, 0, (currentZoomDist - defaultZoomDist) / (maxZoomDist - defaultZoomDist))
end
return math.round(nonRoundedZoomDist)
end
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.