Hey, I’m building a multiplayer version with multiple maps based on a quite popular video game Five Night’s At Freddy’s.
Since I’m scripting a “FNAF Camera” system I’m using ViewportFrames to build a “Minimap” with the walls in my workspace parts completely done with scripting. (Shown with a red arrow in the screenshot)
It’s working fine until I want to use WorldToViewportPoint to get a 2D position of a World position depending of the ViewportCamera. It returns false and don’t give me the position. So the TextButtons (Blue circle) isn’t placed correctly on the small red circles.
I already used WorldToViewportPoint on other projects before including creating a new instance of a camera to check if an NPC sees a player, it looks like Cameras can’t be placed inside ViewportFrames.
-- We create a new camera to put it in the viewportframe
local ViewportCamera = Instance.new("Camera")
ViewportCamera.CameraType = Enum.CameraType.Scriptable
ViewportCamera.FieldOfView = 90
ViewportCamera.CFrame = ((CurrentMap.Gameplay.Roof.CFrame - Vector3.new(0,CurrentMap.Gameplay.Roof.Position.Y,0)) + Vector3.new(0,Height,0)) * CFrame.Angles(-math.rad(90),0,0)
ViewportCamera.Parent = ViewportFrame
ViewportFrame.CurrentCamera = ViewportCamera
-- For each camera (part) in the folder, we get his 2d position using WorldToViewportPoint
for i, camera:BasePart in pairs(CurrentMap.Cameras:GetChildren()) do
local vector:Vector2, onScreen:bool = ViewportCamera:WorldToViewportPoint(camera.Position * Vector3.new(1,0,1))
print(onScreen) -- returns false for absolutely no reasons
local CamButton = Template:Clone()
CamButton.Position = UDim2.fromOffset(vector.X, vector.Y)
CamButton.Text = "CAM ".. i
CamButton.MouseButton1Click:Connect(function()
ViewportFrame.Parent.ChangeCam:Fire(i)
for _,cambutton in pairs(ViewportFrame.Map:GetChildren()) do
CamButton.BackgroundColor3 = Color3.fromRGB(22, 22, 26)
end
CamButton.BackgroundColor3 = Color3.fromRGB(103, 217, 27)
end)
CamButton.Parent = ViewportFrame.Cameras
end