Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Move camera smoothly back when running?

What i'm trying to do is when the player starts sprinting i want the camera to move back smoothly a little bit, and then move back when they stop while still following the player while also make it zoom back a little smoothly. What i have kinda works but it don't follow the player, any way i can do this better?

local TweenService = game:GetService("TweenService")



local Player = game.Players.LocalPlayer

local Character = Player.Character or Player.CharacterAdded:wait()

local Mouse = Player:GetMouse()

local Camera = game.Workspace.CurrentCamera

  

Mouse.Button1Down:Connect(function()

local TweeningInfo = TweenInfo.new(1.25, Enum.EasingStyle.Elastic, Enum.EasingDirection.Out)

local MoveTo = {CFrame = Camera.CFrame - Vector3.new(0,0,1)}

local CameraAnim = TweenService:Create(Camera, TweeningInfo, MoveTo)

CameraAnim:Play()

end)

1 answer

✓ Accepted answer

I personally like to use Camera.FieldOfView to move the camera back. I'm not familiar with TweenService, I just use :Lerp() for all my things. Hopefully this can give you an idea on what you want to achieve.

Humanoid.Running

https://developer.roblox.com/api-reference/event/Humanoid/Running If you want to check the if the player is moving or not, you can use this. This also gives you information on the speed of the player.

Here is an example script on how I would do it:

local moving = false
Humanoid.Running:Connect(function(speed)
    if (speed > 0) then
	    moving = true
    else
	    moving = false
    end
end)

function lerp(a, b, t)
    return a *  (1-t)  +  (b*t)
end

local defaultFOV = 70
local sprintingFOV = 100
RunService.RenderStepped:Connect(function()
    local targetFOV = (moving and sprintingFOV) or (defaultFOV)
    local newFOV = lerp(Camera.FieldOfView, targetFOV, .1)
    Camera.FieldOfView = newFOV
end)

1 comment

0

Thanks a lot for this!

This discussion is preserved as it was. New answers, comments, and votes are closed.