Idea Create a part, and focus the camera on it to use it as a gui background
Problem The script runs and does it fine, but the issue is that the gui doesn't change when a Bool value is changed in workspace? any ideas?
Possibility I think that I need to use a changed function, but I'm not sure how to set that up in this script.
Code
-------------------------------------------------------- -- Setup -------------------------------------------------------- self = script.Parent c = workspace.CurrentCamera rs = game:GetService("RunService") util = LoadLibrary("RbxUtility") Create = util.Create; music = self:WaitForChild("Music") music:Stop() music:Play() game.StarterGui:SetCoreGuiEnabled("PlayerList",false) game.StarterGui:SetCoreGuiEnabled("Health",false) game.StarterGui:SetCoreGuiEnabled("Backpack",false) --------------------------------------------------------- -- Screen Utilities function ScreenSpaceToWorld(x, y, depth) local resolution = self.AbsoluteSize local aspectRatio = resolution.X / resolution.Y local hfactor = math.tan(math.rad(c.FieldOfView)/2) local wfactor = aspectRatio*hfactor local xf, yf = x/resolution.X*2 - 1, y/resolution.Y*2 - 1 local xpos = xf * -wfactor * depth local ypos = yf * hfactor * depth return Vector3.new(xpos, ypos, depth) end function GetDepthForWidth(partWidth, visibleSize) local resolution = self.AbsoluteSize; local aspectRatio = resolution.X / resolution.Y local hfactor = math.tan(math.rad(c.FieldOfView)/2) local wfactor = aspectRatio*hfactor return (-0.5*resolution.X*partWidth/(visibleSize*wfactor)) end --------------------------------------------------------------- ---------------------------------------------------------------- -- Build backdrop local backdrop = Create'Part' { FormFactor = "Custom"; Anchored = true; CanCollide = false; TopSurface = "Smooth"; BottomSurface = "Smooth"; Material = "Neon"; BrickColor = BrickColor.new("Navy blue"); Parent = c; } function updateBackdrop() local x,y = self.AbsoluteSize.X,self.AbsoluteSize.Y local cx,cy = math.floor(x/2),math.floor(y/2) local cz = GetDepthForWidth(40,x) local ratio = y/x local pos = ScreenSpaceToWorld(cx,cy,cz) backdrop.Size = Vector3.new(40,40*ratio,0) backdrop.CFrame = c.CoordinateFrame * CFrame.new(pos) if game.Workspace.v.Value == false then c.CameraType = "Scriptable" c.CoordinateFrame = CFrame.new(9999,9999,9999) if game.Workspace.v.Value == true then c.CameraType = "Follow" end end end updateBackdrop() rs.RenderStepped:connect(updateBackdrop) ------------------------------------------------------------------- -------------------------------------------------------------------
Thanks :D
The changed event would work like this:
updateBackdrop() --First time setup Workspace.v.Changed:connect(function() --Will be called if the value changes updateBackdrop() end)
You will have to create another function for the actual camera movement.
This might work?
-------------------------------------------------------- -- Setup -------------------------------------------------------- self = script.Parent c = workspace.CurrentCamera rs = game:GetService("RunService") util = LoadLibrary("RbxUtility") Create = util.Create; music = self:WaitForChild("Music") music:Stop() music:Play() game.StarterGui:SetCoreGuiEnabled("PlayerList",false) game.StarterGui:SetCoreGuiEnabled("Health",false) game.StarterGui:SetCoreGuiEnabled("Backpack",false) --------------------------------------------------------- -- Screen Utilities function ScreenSpaceToWorld(x, y, depth) local resolution = self.AbsoluteSize local aspectRatio = resolution.X / resolution.Y local hfactor = math.tan(math.rad(c.FieldOfView)/2) local wfactor = aspectRatio*hfactor local xf, yf = x/resolution.X*2 - 1, y/resolution.Y*2 - 1 local xpos = xf * -wfactor * depth local ypos = yf * hfactor * depth return Vector3.new(xpos, ypos, depth) end function GetDepthForWidth(partWidth, visibleSize) local resolution = self.AbsoluteSize; local aspectRatio = resolution.X / resolution.Y local hfactor = math.tan(math.rad(c.FieldOfView)/2) local wfactor = aspectRatio*hfactor return (-0.5*resolution.X*partWidth/(visibleSize*wfactor)) end --------------------------------------------------------------- ---------------------------------------------------------------- -- Build backdrop local backdrop = Create'Part' { FormFactor = "Custom"; Anchored = true; CanCollide = false; TopSurface = "Smooth"; BottomSurface = "Smooth"; Material = "Neon"; BrickColor = BrickColor.new("Navy blue"); Parent = c; } function updateBackdrop() if game.Workspace.v.Value == false then c.CameraType = "Scriptable" c.CoordinateFrame = CFrame.new(9999,9999,9999) elseif game.Workspace.v.Value == true then c.CameraType = "Follow" end end updateCamera = function() if c.CameraType == "Follow" then local x,y = self.AbsoluteSize.X,self.AbsoluteSize.Y local cx,cy = math.floor(x/2),math.floor(y/2) local cz = GetDepthForWidth(40,x) local ratio = y/x local pos = ScreenSpaceToWorld(cx,cy,cz) backdrop.Size = Vector3.new(40,40*ratio,0) backdrop.CFrame = c.CoordinateFrame * CFrame.new(pos) end end updateBackdrop() workspace.v.Changed:connect(function() updateBackdrop() end rs.RenderStepped:connect(updateCamera) -------------------------------------------------------------------