How to force zoom the camera?

How can I force the camera zoom? I mean, when I do something, I want the zoom to change as if you had changed it at will.

1 Like

This code should move the camera forward. I think this is what you want.

local cam = workspace.CurrentCamera
local camCf = cam.CFrame
cam.CFrame = camCf+camCf.LookVector*15 -- change this number to what you want
2 Likes

You can’t access the zoom distance as if they were scrolling. As you can see in Camera | Roblox Creator Documentation it’s locked to roblox core script security. This means only roblox can use it. You’d have to use another method such as positioning the cframe, which the above comment shows how you could achive this.

You will have to loop through the CameraMaxZoomDistance and the CameraMinZoomDistance properties of Player to manually zoom in/out.

2 Likes

I just tested looping CameraMaxZoomDistance and the CameraMinZoomDistance and it doesnt work. I believe this is impossible.

I also tried script like this for the idea of setting the cframe:

            local char = game.Players.LocalPlayer.Character
			local cam = game.Workspace.CurrentCamera
			local zoomYoffset = 5
			local zoomVectorMult = 5
			local target = char.Head

			cam.CameraType = Enum.CameraType.Scriptable
			local p =CFrame.new(target.Position.x,target.Position.y+zoomYoffset/2,target.position.z) * -target.CFrame.LookVector*zoomVectorMult
			cam.CFrame = CFrame.new(p.x,p.y,p.z)
			wait()
			cam.CameraType = Enum.CameraType.Custom
			cam.CameraSubject = char.Humanoid

and that didnt work. To this day I still cant find a answer, I believe the actual solution is to try not build your game requiring setting the camera’s zoom for any people on this forum looking for an answer.

This works, set the CameraMaxZoomDistance the same as CameraMinZoomDistance and itll automatically force the camera to zoom to that distance.

5 Likes

@NUTRICORP, I believe I’ve found a solution, if this is what you’re talking about

You can use the Camera.FieldOfView property to change the zoom factor.

Some things to keep in mind:

  • Its not exactly a regular zoom. Its more like camera staying in place and zooming in.
  • Remeber to set the CameraType to Scriptable before changing, and changing it back to Custom after.
  • Of course, make sure you use a LocalScript.

Hope this helps,
JaxsonRox1

2 Likes

If you go into studio and run an experience, there is a PlayerModule in your game.Players[YourPlayer].PlayerScripts. This module contains information about how roblox controls your camera, it’s hard to decipher the code, but in due time you should be able to figure it out.

Edit: Everything is a ModuleScript, so you can run the functions inside the scripts.

1 Like

It works but the camera is shivering and unstable when used with tween service

Basically what I am trying to achieve is to make a gun that when the player clicks the right mouse button, the camera zooms in to aim. I was successful in making it zoom in by setting the CameraMaxZoomDistance same as CameraMinZoomDistance. But the problem arises when I want it to zoom out. Sometimes the code doesn’t work and sticks to the zoomed in state.
Do you know how to achieve this in a better way?

Here’s my script:

local player = game.Players.LocalPlayer
local RS = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local TS = game:GetService("TweenService")
local held = false
local deb = false
local rs

local TI = TweenInfo.new(

	0.05,
	Enum.EasingStyle.Sine, 
	Enum.EasingDirection.InOut

)

local function shoot()
	
	print("shot")
	wait(0.1)
	
end


function setZoom(min,set,max)
	
	--player.CameraMinZoomDistance=set
	--player.CameraMaxZoomDistance=set 
	--task.wait(0.1)
	--player.CameraMinZoomDistance=min
	--player.CameraMaxZoomDistance=max
	
	local TweenCameraZoomS = TS:Create(player, TI, {CameraMaxZoomDistance = set})
	local TweenCameraZoomE = TS:Create(player, TI, {CameraMinZoomDistance = set})

	TweenCameraZoomS:Play()
	TweenCameraZoomE:Play()

	task.wait(0.1)

	local TweenCameraZoomS = TS:Create(player, TI, {CameraMaxZoomDistance = max})
	local TweenCameraZoomE = TS:Create(player, TI, {CameraMinZoomDistance = min})

	TweenCameraZoomS:Play()
	TweenCameraZoomE:Play()
	
	--task.wait(0.1)
	--player.CameraMinZoomDistance=max
	--player.CameraMaxZoomDistance=max
end




local function click(input, _gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		print("click")
		
		held = true
		
		rs = RS.Stepped:Connect(function()
			
			if deb and held == true then return end
			deb = true
			
			shoot()
			setZoom(5, 5, 5)
			

			deb = false	
		end)
		
		
		
	end
end

local function unclick(input, _gameProcessed)
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		
		held = false
		rs:Disconnect()
		setZoom(11, 9.5, 11)
		wait(0.2)
		setZoom(4.5, 9.5, 10)
	end
	
end

UserInputService.InputBegan:Connect(click)
UserInputService.InputEnded:Connect(unclick)```

EDIT: I had quoted the wrong poster, I was replying to this recent post:

Since what you actually want to do is zoom (like for a sniper scope or binoculars view), you do not need to fork and modify the camera scripts, you can just have your own script in StarterPlayerScripts that only changes workspace.CurrentCamera.FieldOfView, which the Roblox camera scripts do not make use of as far as I know. Classic camera at least keeps FoV at 70 degrees vertical.

In some Roblox properties, like CameraMaxZoomDistance, “zoom” is used incorrectly. Properly, in cinematography terms, moving the camera closer or farther from the subject is called “trucking” and zooming refers only to changing the camera field of view by changing the focal length of a zoom lens, without moving the camera position. If you want to change subject distance, it’s a lot more difficult and is most easily done by forking and editing ClassicCamera to override the mousewheel and touch-pinch code. If you dive into the camera scripts, you need to keep in mind that what is called “zoom” in there is not the same as what you are calling zoom. You are more correct.

1 Like