Setting the Distance at Which the Camera is from the Subject

I’m aware that you can set the camera’s maximum and minimum zoom distances, but I have discovered that there’s a catch: You can’t have the effects be put into action until the player (and this is assuming they even will) decides to scroll with their mouse or pinch their fingers on mobile (presumably).

Basically, I can put distance restrictions on the camera, but they don’t truly “activate” until the player scrolls with their mouse or pinches their fingers. Other than that, all of the code is working fine. Here’s a bit of the code that shows (“ChangeLocation” is a model in the Workspace, and the “camera” inside it is a Part):

					Player.CameraMaxZoomDistance = 0
					Player.CameraMinZoomDistance = 0
					Camera.CameraSubject = ChangeLocation.Camera
					Camera.CameraType = "Attach"
					Camera.CFrame = ChangeLocation.Camera.CFrame
					
					local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
					local goal = {}
					goal.Position = Vector3.new(part.Position.X, part.Position.Y, ChangeLocation.Camera.Position.Z)
					
					local TweenService = game:GetService("TweenService")
					
					local tween = TweenService:Create(ChangeLocation.Camera, tweenInfo, goal)
					
					tween:Play()

It is possible, however, that another solution might be favourable. When the Part begins to tween, the actual camera seems to immediately teleport in what is seemingly a definite amount of studs on the Z axis. Thank you for any advice you can give!

3 Likes

If you change the values while in Studio via StarterPlayer this isn’t an issue.

1 Like

He’s talking about changing the distance during runtime. For example, inside will have a much closer camera than outside.

1 Like

As I know, to set Max zoom to 0, you first need to set min to 0, otherwise max will be equal to min. But if it works well for you, nvm me.

If you set camera CFrame it should handle you, but if it doesn’t, then you can try manipulating Roblox’s own camera script and fire the function that gets fired when mouse wheel is scrolled.

1 Like

I’ll try the second one since I already tried the first. uwu

How would I do this?

  1. Put a BindableEvent in ReplicatedStorage
  2. Fire that BindableEvent from your own LocalScript when you want to.

  1. First, test in solo and copy the CameraScript in Player.PlayerScripts. Paste it directly to StarterPlayerScripts after you stop test.

After this, you can figure it yourself and it could be a nice practice for you. Roblox codes are written by professionals and while trying to edit them, trying to read them always gives you one or more things to learn. But I’ll still write if you can’t find out.


  1. What you are looking for is in RootCamera module(It is in the code you just copied). Line 988. You need to fire that function after you get the news with the bindable’s Event. Function needs to be fired from the inside of the function it is since it is a local function. It’s up to you after this.

PS: If anyone else knows a better and easier way, please tell, it would be lovely to learn for all of us too!

1 Like

Looks like it’s a bug which’s being looked into
Unlock Camera:Zoom(distance) - #5 by nurgenius

1 Like

If you set CameraMaxZoomDistance to lower than it is zoomed in at it will automatically bring your camera in and vice versa. This is a property of Player.
https://www.robloxdev.com/api-reference/class/Player

Might not be what you’re looking for but this is one method of doing it.

That is what I initially tried, but I guess it’s buggy. 3:

Well, you have to remember to set min and max accordingly. For example you want to make it so when you press T you switch between 1st and 3rd person. For third person. you have to set max zoom to let’s say 5 then minimum to 5. Then when you go to first you set min to 0.5 first then max to 0.5 after.

It will work if you do it in order.

I’ve tried that as well. Both ways seem not to function. It’s a bit odd, but I guess I’ll need to use the other guy’s alternative for now. .3.

You changed this value in player right? Not anywhere else? player.CameraMaxZoomDistance?

1 Like

Yes.

This is working for me.

local player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local first = false

player.CameraMaxZoomDistance = 5
player.CameraMinZoomDistance = 5

UIS.InputBegan:Connect(function(input,typing)
if typing then return end

if input.KeyCode == Enum.KeyCode.T then
	if first == false then
		player.CameraMinZoomDistance = .5
		player.CameraMaxZoomDistance = .5
		first = true
	else
		first = false
		player.CameraMaxZoomDistance = 5
		player.CameraMinZoomDistance = 5
		end
	end
end)

Thank you! It worked!

Happy to help!

Not sure, if you saw the script I sent you, but if it works then all is good.

This is a separate bug from what @DarkContinuum posted. We’re working on a fix for this one too.

1 Like

Sorry. Forgot to reply… .3.

It seems that they’re fixing the bug, and I guess it’s only happening in certain situations that I can’t really define. .3.