Answered by
4 years ago Edited 4 years ago
Not sure if this is what your asking but i have made a camera system that uses a ViewportFrame. the script simply clones what the camera sees and uses the viewportframe as a screen.
the code is as follows:
It also might give you some idea on how to move something without using welds! :)
Code(LocalScript):
001 | local function CreateRegion 3 FromLocAndSize(PartBase,Offset, Size) |
002 | local SizeOffset = Size/ 2 |
003 | local CF = PartBase.CFrame*Offset |
004 | local Point 1 = (CF - SizeOffset).p |
005 | local Point 2 = (CF + SizeOffset).p |
006 | return Region 3. new(Point 1 , Point 2 ) |
009 | local RegionSize = Vector 3. new( 250 , 250 , 250 ) |
010 | local Camera = workspace.CurrentCamera |
011 | local Player = script.Parent.Parent |
012 | local PlayerGui = Player.PlayerGui |
014 | local ScreenGui = ( function () |
015 | local Screen_Name = "Camera View Port" |
016 | local S = PlayerGui:FindFirstChild(Screen_Name) |
018 | S = Instance.new( "ScreenGui" ,PlayerGui) |
024 | local Screen = Instance.new( "ViewportFrame" , ScreenGui) |
025 | Screen.Size = UDim 2. new( 0 , 250 , 0 , 250 ) |
026 | Screen.Position = UDim 2. new( 0 , 0 , 1 ,- 250 ) |
027 | Screen.BackgroundColor 3 = Color 3. new( 1 , 1 , 1 ) |
029 | local guiCamera = Instance.new( "Camera" , Screen) |
030 | Screen.CurrentCamera = guiCamera |
031 | local CameraOffset = CFrame.new( 0 , 0 , 0 ) |
035 | local function IsPartInRegion 3 (Part,Region 3 Space) |
036 | local R 3 Size = Region 3 Space.Size |
037 | local X 1 = Region 3 Space.CFrame.X - (R 3 Size.X/ 2 ) |
038 | local Y 1 = Region 3 Space.CFrame.Y - (R 3 Size.Y/ 2 ) |
039 | local Z 1 = Region 3 Space.CFrame.Z - (R 3 Size.Z/ 2 ) |
040 | local X 2 = Region 3 Space.CFrame.X + (R 3 Size.X/ 2 ) |
041 | local Y 2 = Region 3 Space.CFrame.Y + (R 3 Size.Y/ 2 ) |
042 | local Z 2 = Region 3 Space.CFrame.Z + (R 3 Size.Z/ 2 ) |
043 | local LowerCorner = Vector 3. new(X 1 ,Y 1 ,Z 1 ) |
044 | local UpperCorner = Vector 3. new(X 2 ,Y 2 ,Z 2 ) |
045 | return Part.Position.X > LowerCorner.X |
046 | and Part.Position.X < UpperCorner.X |
047 | and Part.Position.Y > LowerCorner.Y |
048 | and Part.Position.Y < UpperCorner.Y |
049 | and Part.Position.Z > LowerCorner.Z |
050 | and Part.Position.Z < UpperCorner.Z |
052 | local function isInView(Camera,Part) |
054 | local function toRot(CF) |
055 | local sx, sy, sz, m 00 , m 01 , m 02 , m 10 , m 11 , m 12 , m 20 , m 21 , m 22 = CF:components() |
056 | local Y = math.atan 2 (m 02 , m 22 )*( 180 /math.pi)+ 180 |
057 | local X = math.asin(-m 12 )*( 180 /math.pi)+ 180 |
058 | local Z = math.atan 2 (m 10 , m 11 )*( 180 /math.pi)+ 180 |
059 | return Vector 3. new(X,Y,Z) |
061 | local DirCF = CFrame.new(Camera.CFrame.p, Part.Position) |
064 | local Rot = toRot(DirCF) |
065 | local ARot = toRot(DirCF*CFrame.Angles( 0 ,math.rad( 180 ), 0 )) |
066 | local MyRot = toRot(Camera.CFrame) |
067 | local Aim = (ARot.Y)-(MyRot.Y) |
069 | local b_CanISeeIt = math.abs(Aim) > Camera.FieldOfView |
076 | local RS = game:GetService( "RunService" ) |
077 | RS.Heartbeat:connect( function () |
078 | local Offset = CFrame.new( 0 , 0 ,- 15 ) |
079 | local R 3 = CreateRegion 3 FromLocAndSize(Camera,Offset, RegionSize) |
081 | for OPart,CPart in pairs (ScreenBits) do |
082 | local bisInView = isInView(Camera,OPart) |
083 | local isInR 3 = IsPartInRegion 3 (OPart,R 3 ) |
084 | if ( not isInR 3 or OPart:IsDescendantOf(workspace) = = false ) then |
085 | ScreenBits [ OPart ] = nil |
087 | elseif ( not bisInView) then |
088 | ScreenBits [ OPart ] = nil |
093 | guiCamera.CFrame = Camera.CFrame*CameraOffset |
094 | for k,v in pairs (workspace:FindPartsInRegion 3 WithIgnoreList(R 3 , { } , 10000 )) do |
095 | local Bit = ScreenBits [ v ] |
097 | local bisInView = isInView(Camera,v) |
098 | if (bisInView = = true ) then |
102 | v.Changed:Connect( function (thing) |
104 | local Exists,err = pcall ( function () return Orig [ thing ] end , nil ) |
107 | MyCne [ thing ] = Orig [ thing ] |
113 | Bit.CFrame = v.CFrame |
just copy and paste that into a local script inside the PlayerStarterscripts and run the game to see the effect! :)
Closed as off-topic by JesseSong
This question has been closed by our community as being off-topic from ROBLOX Lua Scripting.
Why was this question closed?