wait(5)
local RunService = game:GetService("RunService")
local Character = game.Players.LocalPlayer.Character
local Camera = workspace.CurrentCamera
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local XOffset = 0
local YOffset = 0
function lerp(a, b, c)
return a + (b-a) * c
end
RunService:BindToRenderStep("MoveCamera", Enum.RenderPriority.Camera.Value, function(deltaTime)
local flatCamLookVector = (Camera.CFrame.LookVector * Vector3.new(1,1,1)).Unit
-- flat camera vector (no Y axis)
-- So the X and Z movement is relative to the camera
-- but the Y is relative to the world
local flatCamCFrame = CFrame.new(Vector3.zero, flatCamLookVector) -- cframe from the flat vector
local localMovement = flatCamCFrame:VectorToObjectSpace(HumanoidRootPart.Velocity/16) -- turn velocity from world to the flat camera cframe
local xMovement = localMovement.X -- calculate the X movement based on the camera
local yMovement = localMovement.Y -- calculate the Y movement based on the camera
XOffset = lerp(XOffset, xMovement, math.pow(0.2, deltaTime*60)) -- interpolate for smoothness
YOffset = lerp(YOffset, yMovement, math.pow(0.2, deltaTime*60)) -- interpolate for smoothness
-- see https://www.construct.net/blogs/2/924 for delta time implementation
Camera.CFrame = Camera.CFrame * CFrame.new(0, 0, 0) + flatCamCFrame.RightVector*-XOffset + flatCamCFrame.UpVector*-YOffset -- add negative offset to camera CFrame
end)
this code for smooth camera. After the character’s death, the script stops moving the camera smoothly
The reason the camera script stops working when your character dies is because the HumanoidRootPart disappears. When your character dies.
So you can listen for changes to the Character property of the player and reattach the camera script accordingly.
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
local XOffset = 0
local YOffset = 0
function lerp(a, b, c)
return a + (b-a) * c
end
local function onCharacterAdded(character)
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
RunService:BindToRenderStep("MoveCamera", Enum.RenderPriority.Camera.Value, function(deltaTime)
if not humanoidRootPart then return end
local flatCamLookVector = (camera.CFrame.LookVector * Vector3.new(1,1,1)).Unit
-- flat camera vector (no Y axis)
-- So the X and Z movement is relative to the camera
-- but the Y is relative to the world
local flatCamCFrame = CFrame.new(Vector3.zero, flatCamLookVector) -- cframe from the flat vector
local localMovement = flatCamCFrame:VectorToObjectSpace(humanoidRootPart.Velocity / 16) -- turn velocity from world to the flat camera cframe
local xMovement = localMovement.X -- calculate the X movement based on the camera
local yMovement = localMovement.Y -- calculate the Y movement based on the camera
XOffset = lerp(XOffset, xMovement, math.pow(0.2, deltaTime * 60)) -- interpolate for smoothness
YOffset = lerp(YOffset, yMovement, math.pow(0.2, deltaTime * 60)) -- interpolate for smoothness
-- see https://www.construct.net/blogs/2/924 for delta time implementation
camera.CFrame = camera.CFrame * CFrame.new(0, 0, 0) + flatCamCFrame.RightVector * -XOffset + flatCamCFrame.UpVector * -YOffset -- add negative offset to camera CFrame
end)
end
local function onCharacterRemoving()
RunService:UnbindFromRenderStep("MoveCamera")
end
player.CharacterAdded:Connect(onCharacterAdded)
player.CharacterRemoving:Connect(onCharacterRemoving)
-- Initialize with the current character if it exists
if player.Character then
onCharacterAdded(player.Character)
end