I know the title is a little bit weird, but I kind of want to improve on my scripting, and personally I think I can improve step by step. I kind of don’t know how to do something like this and I need some help.
You can achieve the effect by lerping the Camera’s FOV property. Here’s a quick example.
local function Lerp(a, b, d) return a + (b - a) * d end
local CAMERA = workspace.CurrentCamera
-- change the FOV value based on whether or not something was clicked.
-- i is the current step for the lerp (which has a range from 0 to 1, inclusive. just change that to whatever step you're in within a simple for loop or whatever.
CAMERA.FieldOfView = Lerp(CAMERA.FieldOfView, FOV, i)
You can achieve this by taking use of TweenService and UserInputService.
In theory you would once right/left mouse button or any other input method is pressed use the method delay(n, function) when the zoom-in has initiated to wait a specific time amount before zooming out again.
Defining constants and variables
local Camera = workspace.CurrentCamera
local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
local ZoomedIn = false
local ZoomInTime, ZoomOutTime = .2, .5
local ZoomInFOV, ZoomOutFOV = 50, 70
local ZoomOutDelay = 2
Zoom in/out function
function CameraZoom(bool)
local tweenInfo = TweenInfo.new(bool and ZoomInTime or ZoomOutTime)
local tweenProperties = {FieldOfView = bool and ZoomInFOV or ZoomOutFOV}
TweenService:Create(Camera, tweenInfo, tweenProperties):Play()
end
Registering user input
function CameraZoomIn() -- additional function so you can use the CameraZoom function to your liking
if not ZoomedIn then
ZoomedIn = true
CameraZoom(true)
delay(ZoomOutDelay, function()
ZoomedIn = false
CameraZoom(false)
end)
end
end
UserInputService.InputBegan:Connect(function(input, GameProcessedEvent)
if GameProcessedEvent then return end -- prevents the input to pass through if typing in a textbox for e.g. a chat
if input.UserInputType == Enum.UserInputType.MouseButton1 then -- MouseButton1Down
CameraZoomIn()
elseif input.KeyCode == Enum.KeyCode.Q then -- Key down
CameraZoomIn()
end
end)
[color=#FFE300] Note: This code is stored in a LocalScript parented to game.StarterPlayer.StarterCharacterScripts. [/color]
You can add a cooldown by saving what the tick was when you zoomed in, then afterwards compare the current tick() and the saved tick() to check if the delta of the two ticks is greater than a certain amount.
Add to the variables
local ZoomCooldown = 3
local PrevZoomIn = 0
Modified zoom in/out function
function CameraZoomIn() -- additional function so you can use the CameraZoom function to your liking
local DeltaTick = tick() - PrevZoomIn
if not ZoomedIn and DeltaTick >= ZoomCooldown then
PrevZoomIn = tick()
ZoomedIn = true
CameraZoom(true)
delay(ZoomOutDelay, function()
ZoomedIn = false
CameraZoom(false)
end)
end
end