Hello, I am trying to make a minimap for my FPS game however I want to make a viewportframe display the map in real time without having to clone the entire map into the viewportframe. I have already written a script that should do everything I just need to make it display 3000 studs up from the local players position on to the viewportframe.
Here’s the script:
local camera = Instance.new("Camera", game.Workspace)
camera.CameraType = "Scriptable"
script.Parent.CurrentCamera = camera
camera.FieldOfView = 1
local plr = game.Players.LocalPlayer
repeat task.wait() until plr.Character:FindFirstChild("HumanoidRootPart")
print("starting")
local rp = plr.Character.HumanoidRootPart
game:GetService("RunService").RenderStepped:Connect(function()
camera.CFrame = CFrame.new(Vector3.new(rp.Position.X, rp.Position.Y + 3000, rp.Position.Z))
script.Parent.ImageLabel.Rotation = rp.Orientation.Y - 90
end)
I don’t really like to use other people’s code because it’s unprofessional. When I mean “other people’s code” I don’t mean that I can’t get advice on what I already have, I just don’t want to be completely spoonfed.
They’re used for putting objects in the picture without having to make a free model, but if you want it to match the camera, turn off IgnoreGuiInset and put a localscript on the frame and use this
local RunService = game:GetService("RunService")
RunService.RenderStepped:Connect(function()
script.Parent.CurrentCamera = workspace.CurrentCamera
end)
Hello! ViewportFrames are a way to show 3D objects in the user interface space.
In my minimap I use a viewportframe to display my map. I rotate the camera to make rotations possible, and get the positions of the blips using this code made by EgoMoose.
local function pointToViewport(camera, p, vps) --This is written EgoMoose :D
local lp = camera.CFrame:pointToObjectSpace(p); -- make point relative to camera so easier to work with
local r = vps.x/vps.y; -- aspect ratio
local h = -lp.z*math.tan(math.rad(camera.FieldOfView/2)); -- calc height/2
local w = r*h; -- calc width/2
local corner = Vector3.new(-w, h, lp.z); -- find the top left corner of the far plane
local relative = lp - corner; -- get the 3d point relative to the corner
local sx = relative.x / (w*2); -- find the x percentage
local sy = -relative.y / (h*2); -- find the y percentage
local onscreen = -lp.z > 0 and sx >=0 and sx <= 1 and sy >=0 and sy <= 1;
-- returns in pixels as opposed to scale
return Vector2.new(sx*vps.x, sy*vps.y);
end;
I do some more stuff, but that is essentially it. Feel free to take a look at my code. You don’t have to use my scripts, but you could still learn something.
I would also recommend this view by Sleitnick on viewportframes and fitting models into them, to get an even better understanding on them.
It is not unprofessional to use other peoples code. Most programmers does it, or has done it atleast once in their career. It is ofcourse most of the time only small snippets of code, but it could also be a whole framework or library which makes the development a little easier / faster.