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

How to change camera in different places?

Asked by 8 years ago

So I have a 2D camera setup for my platformer, when I teleport to another place, I want that camera to be in its normal state, but if i teleport to another level, the camera needs to go back to 2D mode

--This is a normal script named TwoD
cam = workspace.CurrentCamera

lockCamera = true
    distance = 30
    height = 5

function GetMass(object)
    local mass = 0
    if pcall(function() return object:GetMass() end) then
        mass = object:GetMass()
    end
    for _,child in pairs(object:GetChildren()) do
        mass = mass + GetMass(child)
    end
    return mass
end

function onPlayerRespawned(newPlayer)
    wait()
    torso = newPlayer.Character.Torso 
    bp = Instance.new("BodyPosition")
    bp.Name = "TwoD"
    bp.position = torso.Position
    bp.P = 1000000
    bp.D = 1000
    bp.maxForce = Vector3.new(0, 0, 1000000)
    bp.Parent = torso
    local bf = Instance.new("BodyForce")
    bf.force = Vector3.new(0, 0*GetMass(newPlayer.Character), 0)
    bf.Parent = torso
    script.LocalScript:Clone().Parent = torso.Parent
    newPlayer.Character.DescendantAdded:connect(function()
        bf.force = Vector3.new(0, 0*GetMass(newPlayer.Character), 0)
    end)
    newPlayer.Character.DescendantRemoving:connect(function()
        bf.force = Vector3.new(0,0*GetMass(newPlayer.Character), 0)
    end)
end

function onPlayerEntered(newPlayer)
    if newPlayer.Character then
        onPlayerRespawned(newPlayer)
    end
    newPlayer.Changed:connect(function (property)
        if (property == "Character") and newPlayer.Character then
            onPlayerRespawned(newPlayer)
        end
    end)
end

game.Players.PlayerAdded:connect(onPlayerEntered)


-- This is a LocalScript (child of TwoD above)
lockCamera = true
    distance = 30
    height = 5

local torso = script.Parent.Torso
local center = Instance.new("Part")
center.Name = script.Parent.Name .. " Center"
center.Transparency = 1
center.CanCollide = false
center.Size = Vector3.new(1,1,1)
center.Position = torso.Position
center.CFrame = CFrame.new(Vector3.new(0,0,0),Vector3.new(0,0,-1))
center.Parent = game.Workspace
local bp = Instance.new("BodyPosition")
bp.position = center.Position
bp.maxForce = Vector3.new(1000000, 1000000, 1000000)
bp.Parent = center
local cam = game.Workspace.CurrentCamera
cam.CameraSubject = center
cam.CameraType = Enum.CameraType.Attach

while torso.Parent do
    wait()
    center.BodyPosition.position = torso.Position
    if lockCamera then
        cam.CoordinateFrame = CFrame.new(Vector3.new(center.Position.x + distance,center.Position.y + height,center.Position.z))
    end
end


center:Remove()

Answer this question