I have made an over-the-shoulder camera script which can be toggled via user input. When the player presses a key, a renderstepped connection is set-up to constantly update the camera’s CFrame to the shoulder of the player.
This works, however when the player dies and goes to toggle the camera once again on spawn the
camera’s CFrame is strangely set to where the player died?
CODE:
local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")
local toggle = false
local CONNECTION
local cam = workspace.CurrentCamera
--------------------------------
local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Char:WaitForChild("Humanoid")
local RootPart = Char:WaitForChild("HumanoidRootPart")
----------------------------------
local function CheckRelevantInput(input)
return(input == Enum.KeyCode.V)
end
local CameraTypes = {
Custom = Enum.CameraType.Custom;
Scriptable = Enum.CameraType.Scriptable;
}
cam.CameraType = Enum.CameraType.Custom
local function MoveCamera(input, _)
if input.UserInputType == Enum.UserInputType.MouseMovement then
Humanoid.AutoRotate = false
local input = input.Delta.X
RootPart.CFrame = RootPart.CFrame * CFrame.Angles(0,math.rad(-input/5),0)
end
end
local function CameraChange()
if toggle == true then
cam.CameraType = CameraTypes["Scriptable"]
UIS.MouseBehavior = Enum.MouseBehavior.LockCenter
CONNECTION = UIS.InputChanged:Connect(MoveCamera)
RS:BindToRenderStep("CameraFollow", 1, function()
cam.CFrame = RootPart.CFrame * CFrame.new(2,2,5)
end)
else
Humanoid.AutoRotate = true
cam.CameraType = CameraTypes["Custom"]
RS:UnbindFromRenderStep("CameraFollow")
CONNECTION:Disconnect()
CONNECTION = nil
end
end
UIS.InputBegan:Connect(function(input, GPE)
if GPE or not CheckRelevantInput(input.KeyCode) then
return;
end
toggle = not toggle
CameraChange()
end)
Humanoid.Died:Connect(function()
if CONNECTION then
CONNECTION:Disconnect()
RS:UnbindFromRenderStep("CameraFollow")
cam.CameraType = CameraTypes["Custom"]
end
end)