Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with camera manipulation?

Asked by 4 years ago

I'd like resources or and advice on how to manipulate a camera to do something similar to the following: -The camera to zoom out slightly when the player is sprinting

I am not familiar with manipulating cameras so any advice or resources would be very appreciated

Thank you

1 answer

Log in to vote
1
Answered by
Nanomatics 1160 Moderation Voter
4 years ago

In order to achieve that you can use something called 'TweenService' which does smooth transitioning between anything really. It's a very neat service ROBLOX has. You can use TweenService on things such as:

A number

A bool value

A CFrame

A Rect

A Color3

A UDim

A UDim2

A Vector2

A Vector2int16

A Vector3

More on TweenService

The second thing we will use is the property of the player's camera called 'FieldOfView' which "sets how many degrees in the vertical direction (y-axis) the camera can view. Uniform scaling is enforced meaning the vertical and horizontal field of view are always related by the aspect ratio of the screen. This means the FieldOfView property also determines the horizontal field of view".

More on this here.

This FieldOfView property, when modified, will give the illusion of zooming out/in.

I was bored so I did most of the work for you:

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local tweenInfo = TweenInfo.new(
    0.5, -- how long it takes to tween
    Enum.EasingStyle.Quad,
    Enum.EasingDirection.Out,
    0, -- will it repeat? If below 0 it will go forever
    false -- will it reverse? True or false 
)

local LocalPlayer = Players.LocalPlayer
repeat wait() until LocalPlayer.Character
local Camera = workspace.CurrentCamera

local shiftDown = false

UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.Keyboard and not gameProcessed then
        if input.KeyCode == Enum.KeyCode.LeftShift then
            shiftDown = true
            local goal = {FieldOfView = 100}
            local tween = TweenService:Create(Camera, tweenInfo, goal)
            tween:Play()
            --Zoom out
            --Make the player run -- simple but I dont wanna do the whole thing for you
        end
    end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.LeftShift then
            shiftDown = false
            local goal = {FieldOfView = 70}
            local tween = TweenService:Create(Camera, tweenInfo, goal)
            tween:Play()
            --Zoom in
            --Make the player stop running -- simple but I dont wanna do the whole thing for you
        end
    end
end)

Fun fact: The default camera FieldOfView is 70

Anyways hope I helped, please let me know if you have further questions!

0
Thanks for the detailed answer! I will make sure to use this. avengerstar 74 — 4y
Ad

Answer this question