Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make the camera zoom in on an object when looking at it?

I making a first person game, im not SUPER experienced but know some things. how do i make the camera zoom in on an object when the player looks at it? I have 0 clue...

1 comment

0

Hey I’m going to send you a friends req on Roblox so we can head into studio

2 answers

While I don't believe there is a Zoom property, you can use the Camera.FieldOfView to sort of simulate a zoom effect.

The official documentation says this about the uses of FieldOfView:

  • Reducing FOV to give the impression of magnification (for example when using binoculars)
  • Increasing FOV when the player is ‘sprinting’ to give the impression of a lack of control

Set a minimum angle and make the FoV decrease if the camera angle is a few degrees with the part

local minAngle = math.rad(30) --min Angle
local part = workspace.MyPart -- your part
local currentCamera = workspace.CurrentCamera
local TweenService = game:GetService("TweenService")
local In = TweenService:Create(currentCamera, TweenInfo.new(1), {FieldOfView = 50})
local Zoom = false
local Out = TweenService:Create(currentCamera, TweenInfo.new(1), {FieldOfView = 70}) -- default FieldOfView


game:GetService("RunService").RenderStepped:Connect(function()
	local partPosition = part.CFrame.p
	local cameraCFrame = currentCamera.CFrame
	local cameraPosition = cameraCFrame.p
	local relative = partPosition - cameraPosition
	local out = relative.Unit:Dot(cameraCFrame.LookVector) < math.cos(minAngle)
	
	if not out then
		In:Play()
		Zoom = true
	else
	    if Zoom then 
	         Out:Play() 
	         Zoom = false
	    else return
		end
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.