How to get current camera zoom level

Im trying to make a thing where player’s camera looks at a certain object which I did by manipulating camera’s CFrame. However, I would like to make it that player also can zoom in and out which in my guess can be done by setting player’s current camera zoom level to CFrame’s Z value.
Is there any way of doing that?

2 Likes

Focus position - position i guess

local Camera = workspace.CurrentCamera
local ZoomLevel = (Camera.Focus.Position - Camera.CFrame.Position).Magnitude
3 Likes

You can use the Camera.FieldOfView property to control the zoom level of a camera. The FieldOfView property is a value in degrees that represents the angular field of view of the camera, which determines how much of the scene is visible through the camera.

To zoom in or out with a camera, you can simply change the FieldOfView property by a certain amount. For example, you could use code like this to zoom in:

local camera = workspace.CurrentCamera
local fieldOfView = camera.FieldOfView

fieldOfView = fieldOfView - 5
camera.FieldOfView = fieldOfView

This will decrease the field of view by 5 degrees, effectively zooming in on the scene. To zoom out, you can increase the field of view instead of decreasing it.

It’s also important to note that the FieldOfView property has a maximum and minimum value that it can be set to. The maximum value is 120 degrees, and the minimum value is 1 degree. If you try to set the FieldOfView property to a value outside of this range, it will be clamped to the nearest valid value.

That’s not what I wanted. I want to get player’s current zoom level. Not how to zoom in/out his camera.

I tried it but it doesn’t work. Probably because the camera is locked in one place which causes focus to be in the same place too.

I guess there’s a lot of different ways you can do this, but i think what you can do is since the camera is focused on the head, you could get the distance in the Z axis between the camera and the head.

located in StarterCharacterScripts:

local RunService = game:GetService("RunService")
local cam = workspace.CurrentCamera
local head = script.Parent:WaitForChild('Head')

RunService.RenderStepped:Connect(function()
   -- zAxis is basically the equivalent of saying "Vector3.new(0,0,1)"
     local camZ = Vector3.zAxis *  cam.CFrame.Position
      local headZ = Vector3.zAxis * head.Position
      local magnitude = (camZ - headZ).Magnitude
      print('current zoom level: '..magnitude)
end)

not tested btw since im on a phone, which also explains the bad formatting

2 Likes

That’s not gonna work. The camera position is literally locked, so the ways that use distance between camera position and some object won’t work.
My only guess is that there no way for that or I will have to change roblox’s camera scripts. :person_shrugging:

1 Like

It isnt locked, who even told you that?

1 Like

If it was really locked then how do you explain 2D cam systems?

1 Like

I don’t know. But the point is, in the RenderStepped loop I update the camera CFrame which cause it to be stuck in one place.

1 Like

Just try it man im confident that it’ll work :slight_smile:

1 Like

Doesn’t work. Camera just flies away. Maybe the problem is with my code, but I don’t see any problem in it.

1 Like

My code just reads the Z value of the camera, maybe theres something wrong with one of your scripts

1 Like

I will just send it here.
It’s basically lock on script

RunService:BindToRenderStep("LockOnStep", 199, function(DeltaTime)
                --Your part method
				local CamZ = Vector3.zAxis * Camera.CFrame.Position
				local HeadZ = Vector3.zAxis * Head.Position
				local ZoomLevel = (Head.Position.Z - Camera.CFrame.Position.Z)
				
				local Distance = (HumanoidRootPart.Position - TargetHRP.Position).Magnitude --Distance between player and enemy
                local NewPosition = CFrame.new(HumanoidRootPart.Position, TargetHRP.Position) * CFrame.new(5, 3, ZoomLevel) --Line where it uses it. Camera position.
				local NewDirection = CFrame.new(NewPosition.Position, Head.Position) * CFrame.new(0, 0, -(Distance / 2))  --Direction of the camera. Looks at the middle between player and enemy
				local Goal = CFrame.new(NewPosition.Position, NewDirection.Position) --Sets up new camera CFrame
				
				local Raycast = workspace:Raycast(Head.Position, -NewDirection.LookVector * (Goal.Position - Head.Position).Magnitude, BasicInfo.VFXParams) --Raycast for anti wall phasing
				
                --Making new camera CFrame
				if Raycast then
					NewDirection = CFrame.new(Raycast.Position, Head.Position) * CFrame.new(0, 0, -(Raycast.Position - TargetHRP.Position).Magnitude / 2)
					Goal = CFrame.new(Raycast.Position, NewDirection.Position)
				end
				
				Camera.CFrame = Camera.CFrame:Lerp(Goal, DeltaTime * 10) --Lerping camera to the new position

				--No idea why I just don't put it at the top and do return.
                --Making player face at the enemy
				if Humanoid.AutoRotate == true then
					HumanoidRootPart.CFrame = CFrame.new(HumanoidRootPart.Position, Vector3.new(Head.Position.X, HumanoidRootPart.Position.Y, Head.Position.Z))
				end
			end)
1 Like

My thought is to record the CFrame of the camera relative to the player’s Head’s CFrame. Then change the CameraType to scriptable and manipulate it to however you want. And then when you’re done, set it back to whatever CameraType it was and use the recorded cframe to put it back.

1 Like

Lerps have 3 parameters, not two parameters, first one is the start position, second is the end position, third is the alpha time.

1 Like

Camera just flies away. I guess it doesn’t matter when the zoom level was taken.

1 Like


It says the opposite.
However I still tried it, it just gives an error.
изображение

I guess the start position you are talking about is Camera.CFrame (the thing before :Lerp())

1 Like

Oh my bad im used to Lerp functions, the thing is that you didnt do the alpha time correctly, if you want it to smoothly go towards it and being frame dependent, then this is the math for it:
t = time
1 - t ^ DeltaTime

1 Like

What is the t?

30 symbols

1 Like