I have a system for a 2D camera that follows the player that I can set to different positions and offsets depending on their zone. I want to make it so when the player switches to zone 4 the camera just returns to the default but no matter how much I try it will not work.
The problems I am running into is that I cannot get the camera to return to its normal state, at most I get it to position itself behind the player one time, not follow them, and cant be controlled.
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
local cameraEvent = ReplicatedStorage:WaitForChild("changecamera")
local zones = {
[1] = {
offset = Vector3.new(10, 0, 0),
rotation = Vector3.new(0, 90, 0),
},
[2] = {
offset = Vector3.new(0, 25, -20),
rotation = Vector3.new(-45, 180, 0),
},
[3] = {
offset = Vector3.new(0, 0, -10),
rotation = Vector3.new(0, 180, 0),
},
[4] = {},
}
local currentZone = 1
local smoothness = 0.05
local currentCameraPosition = Vector3.new()
local MoveDirection = Vector3.new()
local speed = 12
cameraEvent.OnClientEvent:Connect(function(zoneNumber)
if zones[zoneNumber] then
currentZone = zoneNumber
local humanoid = player.Character and player.Character:FindFirstChildOfClass("Humanoid")
if zoneNumber == 4 then
camera.CameraType = Enum.CameraType.Custom
camera.CameraSubject = humanoid
camera.FieldOfView = 70
if humanoid and humanoid.Parent then
camera.CFrame = CFrame.new(
humanoid.Parent:GetPivot().Position)
* CFrame.new(0, 2, -8)
end
return
end
camera.CameraType = Enum.CameraType.Scriptable
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
local rootPart = player.Character.HumanoidRootPart
local zone = zones[currentZone]
currentCameraPosition = rootPart.Position + zone.offset
end
else
warn("Zone " .. tostring(zoneNumber) .. " not defined in zones table!")
end
end)
player.CharacterAdded:Connect(function(character)
character:WaitForChild("Humanoid")
if currentZone == 4 then
local humanoid = character:FindFirstChildOfClass("Humanoid")
camera.CameraType = Enum.CameraType.Custom
camera.CameraType = Enum.CameraType.Custom
camera.CameraSubject = humanoid
camera.FieldOfView = 70
camera.CFrame = CFrame.new(character:GetPivot().Position) * CFrame.new(0, 2, -8)
end
end)
RunService.RenderStepped:Connect(function(dt)
if currentZone == 4 then return end
local character = player.Character
if not character then return end
local zone = zones[currentZone]
if not zone then return end
local target = character:GetPivot().Position + zone.offset
local alpha = math.clamp(1 - math.exp(-speed * dt), 0.05, 1)
currentCameraPosition = currentCameraPosition
and currentCameraPosition:Lerp(target, alpha)
or target
local baseCF = CFrame.new(currentCameraPosition)
local rotationCFrame = CFrame.fromOrientation(
math.rad(zone.rotation.X),
math.rad(zone.rotation.Y),
math.rad(zone.rotation.Z)
)
camera.CFrame = baseCF * rotationCFrame
end)
I hope this is a simple problem and I just lack understanding of how these systems work. Any help and feedback would be appreciated.
Thanks
TLDR: When switching the camera from scriptable to custom the camera does not function as intended no matter what I do
There is a gray box to the right of where you spawn that switches your zone to 4