Hello everyone, I am making a 2D-esque type of game. I made a script that locks the camera behind the player, so it feels more like a 2D game. However, I still want the player to be able to zoom in and out, but I do not know how to do this. My script is as follows:
local CAMERA_OFFSET = Vector3.new(-15,20,0)
camera.CameraType = Enum.CameraType.Scriptable
local function onRenderStep()
if player.Character then
local playerPosition = player.Character.HumanoidRootPart.Position
local cameraPosition = playerPosition + CAMERA_OFFSET
-- make the camera follow the player
camera.CoordinateFrame = CFrame.new(cameraPosition, playerPosition)
end
end
RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, onRenderStep)
You can achieve this by listening for mouse wheel movement then directly changing “CAMERA_OFFSET” to make the camera zoom in or out.
The script I gave you also has clamping which stops the camera from zooming too in or too far.
local UIS = game:GetService("UserInputService")
local PS = game:GetService("Players")
local RNS = game:GetService("RunService")
local CAMERA_OFFSET = Vector3.new(-15,20,0)
local player = PS.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local HRP = char:WaitForChild("HumanoidRootPart")
local camera = workspace.CurrentCamera
local function OnRenderStep()
if char then
local playerPos = HRP.Position
local camPos = playerPos + CAMERA_OFFSET
camera.CFrame = CFrame.new(camPos, playerPos)
end
end
local function OnScrollWheel(input)
if input.UserInputType == Enum.UserInputType.MouseWheel then
local pos = input.Position.Z
local x,y
if pos > 0 then
x, y = CAMERA_OFFSET.X + -.5, CAMERA_OFFSET.Y + .5
CAMERA_OFFSET = Vector3.new(if x < -5 and x > -20 then x else CAMERA_OFFSET.X, if y > 10 and y < 25 then y else CAMERA_OFFSET.Y, 0)
else
x, y = CAMERA_OFFSET.X - -.5, CAMERA_OFFSET.Y - .5
CAMERA_OFFSET = Vector3.new(if x < -5 and x > -20 then x else CAMERA_OFFSET.X, if y > 10 and y < 25 then y else CAMERA_OFFSET.Y, 0)
end
end
end
UIS.InputChanged:Connect(OnScrollWheel)
RNS:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, OnRenderStep)
It moves the camera up and down instead. Also, I put this in the invisicam script so I could have invisicam while still being able to customize the camera, although that shouldn’t change anything.
edit: Actually, I just tried it in a blank baseplate, and it worked…