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

How to tween a camera FOV?

Asked by 4 years ago

I want to make shift to sprint and make FOV to big when player sprint so i tween a camera FOV but it's not working

this is a localscript I place this script in starter character script

-- Services
local tweenService = game:GetService("TweenService")

-- Variables
local userInput = game:GetService("UserInputService")
local cam = workspace.CurrentCamera

userInput.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        tweenService:Create(cam, TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 80})
        script.Parent.Humanoid.WalkSpeed = 30
    end
end)

userInput.InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        tweenService:Create(cam, TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 70})
        script.Parent.Humanoid.WalkSpeed = 16
    end
end)

thank you in advance

0
How about using a for loop CaIcuIati0n 246 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

The problem is that you're not playing the Tween. A Tween object has a Play() function which does what you think; it plays the tween.

However, to accomplish this we will need to reference the tween somehow, so we just save it as a local variable.

-- Services
local tweenService = game:GetService("TweenService")

-- Variables
local userInput = game:GetService("UserInputService")
local cam = workspace.CurrentCamera

userInput.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        local t = tweenService:Create(cam, TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 80})
    t:Play()
        script.Parent.Humanoid.WalkSpeed = 30
    end
end)

userInput.InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        local t = tweenService:Create(cam, TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 70})
    t:Play()
        script.Parent.Humanoid.WalkSpeed = 16
    end
end)

Even with this, there's still room for improvement. If a player were to stop running shortly after having started running it will look a bit weird. So to fix this you simply use the Cancel() function of the Tween object to reset the animation.

I used some variables so that each tween is "Saved" but also readable by the entire script, thus solving our problem.

-- Services
local tweenService = game:GetService("TweenService")

-- Variables
local userInput = game:GetService("UserInputService")
local cam = workspace.CurrentCamera
local t0 -- New variable
local t1 -- Also new variable

userInput.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        t0 = tweenService:Create(cam, TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 80})
    t0:Play()
    if t1 then -- Make sure that the variable is set
        t1:Cancel()
    end
        script.Parent.Humanoid.WalkSpeed = 30
    end
end)

userInput.InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        t1 = tweenService:Create(cam, TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 70})
    t1:Play()
    t0:Cancel() -- We don't need an if statement here, since t0 will be defined before it needs to be called.
        script.Parent.Humanoid.WalkSpeed = 16
    end
end)
0
Thank You!! Im forget to add play :) EmangGek 41 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

For Loops

for loops are an easier way to tween stuff, instead of using tweenservice;

for i = 0,1,.01 do
        workspace.CurrentCamera.FieldOfView = i
        wait()
end)

You can also do it backwards

for i = 1,0,-.01 do
      workspace.CurrentCamera.FieldOfView = i
        wait()
end)
0
is for loop recommended by roblox?? EmangGek 41 — 4y

Answer this question