Making the camera zoom in on KeyPress?

Hey!

I am trying to make a system, where when the player presses ‘E’, they speed up for 7 seconds, then a cooldown for 12 seconds. I have successfully completed that, but I also wanted to know how to make it so, as well as the speed boost, the player’s camera zooms in a little automatically.

This is the script
-- Make the speed boost play
local Player = game.Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character.Humanoid
local UserInputService = game:GetService("UserInputService")


local Debounce = true
local key = "E"

UserInputService.InputBegan:Connect(function(Input, IsTyping)
if IsTyping then return end
if Input.KeyCode == Enum.KeyCode[key]and Debounce == true then
    Humanoid.WalkSpeed = 24
Debounce = false
wait(7)
Humanoid.WalkSpeed = 16
wait(12)
Debounce = true

end
end)

Any ideas on where to start?

4 Likes

From what I know there is no official way to set how much the player’s camera is zoomed, there is a Camera.Zoom property which can only be used by roblox core scripts. One hacky way to do it, is to set the StarterPlayer.CameraMaxZoomDistance and StarterPlayer.CameraMinZoomDistance to the same value, and that value is how much you wanna zoom.

Although, a better way to create the effect that I think you’re imagining, is to mess with the camera’s FoV (Field of View). The default value is 70, setting it to something higher will create the speed effect.

4 Likes

Would this fit into my current script? Or a separate script?

The better way to make a Smooth zoom in and zoom out would be changing the Field of view of the localplayer using TweenService.

Note: Everything goes inside a localscript.

--> Services
local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")

--> Static Variables
local Cam = game.Workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character.Humanoid
local Debounce = true
local key = "E"

--> Let's make everything outside so we don't create everytime a tween pressing E
--> (You can edit the Linear style to "Sine" or "Quart" etc...)

local Info = TweenInfo.new(1, Enum.EasingStyle.Back, Enum.EasingDirection.Out, 0, false, 0)

--> Creating the properties that will change in the Tween.
local Start = {FieldOfView = 90} --> This is how much zoom you want :)
local End = {FieldOfView = 70} --> This is Default Zoom (Field of View)

--> Code View
UserInputService.InputBegan:Connect(function(Input, IsTyping)
if IsTyping then return end
if Input.KeyCode == Enum.KeyCode[key] and Debounce then
Debounce = false

Humanoid.WalkSpeed = 24
TweenService:Create(Cam, Info, Start):Play()

wait(7) --> Time that you will stop running

Humanoid.WalkSpeed = 16
TweenService:Create(Cam, Info, End):Play()

wait(12) --> Time to be able to press E again.
Debounce = true

end
end)
11 Likes