Dolly Zoom in ROBLOX

Dolly zooms are cool. I watched some TV show today that used it and instantly wanted to figure out how to do it. Thankfully, it’s not that hard! Simple trigonometry (seriously). I put together a demo place that utilizes the cool effect. I unlocked the place too!

For those that don’t know what a dolly zoom is, it’s when the camera’s field-of-view “zooms” in on the subject, but the subject’s size doesn’t change. You do this by adjusting both the FOV and the camera’s distance from the subject.


Here’s the math that I worked out quickly:


Angle A is where the camera is. Side b is the distance from the camera to the subject. Side a is a pre-determined width from the subject to the edge.

When we change the Field Of View, we calculate side b from the constant side a, which gives us our cool dolly zoom effect.


Here's the code to it

(Refer to my math above for references to variable names)

local cam = game.Workspace.CurrentCamera
cam.CameraType = Enum.CameraType.Scriptable

local origin = game.Workspace.Part1
local focus = game.Workspace.Part2

origin.Transparency = 1
focus.Transparency = 1

local b = (origin.Position - focus.Position).Magnitude
local a = b * math.tan(math.rad(cam.FieldOfView * 0.5))

local tanOfA = math.tan(math.rad(cam.FieldOfView * 0.5))

-- Angle and distance calculations:
cam.Changed:Connect(function(property)
	if (property == "FieldOfView") then
		tanOfA = math.tan(math.rad(cam.FieldOfView * 0.5))
		b = a / tanOfA
	end
end)

-- Continuous camera update:
game:GetService("RunService"):BindToRenderStep("Test", Enum.RenderPriority.Camera.Value, function()
	local pos = focus.Position + (CFrame.new(focus.Position, origin.Position).lookVector * b)
	cam.CFrame = CFrame.new(pos, focus.Position)
end)

Now it will calculate the correct distance when the FieldOfView property is changed.

Note: I imagine someone else has probably done this before, but I haven’t seen a post about it here before, so I figured I’d post this anyway.

28 Likes

I laughed when I saw the SOH-CAH-TOA on the white board. Pretty cool stuff.

7 Likes

lol yeah I basically have to do that or else I won’t ever remember

4 Likes

Some old hippie cought another hippie tripping on acid.

2 Likes

I saw this post last night and was like “…what?” I finally just got it.

1 Like

I pulled off this effect a year back based on some dolly zoom guide I found for Unity.

1 Like

that actually makes me nauseous

1 Like

I made the effect a little too extreme lol.

1 Like

I’m wondering if there would ever be a good use of this effect for anyone. Perhaps part of some cutscene for some horror game.

Hah, I remember coming accross this effect accidentally, while making a 2D camera, that would adjust distance to the screen block, to align it with user’s screen.

That’s a great idea to use this for a cutscene. I imagine using it for sort of a comedic plot twist, zoom-in, moment, would be hilarious.

1 Like