Getting the 3d position of a 2d position

I’m trying to get the 3D position of a 2D position but I keep failing… I’ve been trying this for over 2 days now… Basically, I’m trying to make a GPS system where if you click anywhere on the minimap, it will make a marker there and a part at that location in the real world. This sounds way too complex and it is very complex. I even made two posts about it and seriously no luck. Here’s my code:

local GPS_BUTTON = script.Parent;

local player  = game:GetService("Players").LocalPlayer
local Mouse = player:GetMouse()

local location = script.Parent.Parent:WaitForChild("Location")

local UserInputService = game:GetService("UserInputService")

local char = player.Character or player.CharacterAdded:Wait()

local humanoid = char:WaitForChild("Humanoid")

local locations = {};

humanoid.Touched:Connect(function(hit)
    if hit.Name == "GPS" then
        hit:Destroy()

        for index, location in ipairs(locations) do
            location:Destroy()
        end

        locations = {}
    end
end)

GPS_BUTTON.MouseButton1Click:Connect(function()
    if game.Workspace:FindFirstChild("GPS") then return end

    local minX, minY = GPS_BUTTON.AbsolutePosition.X, GPS_BUTTON.AbsolutePosition.Y

    local maxX, maxY = minX + GPS_BUTTON.AbsoluteSize.X, minY + GPS_BUTTON.AbsoluteSize.Y

    local mouseX = math.clamp(UserInputService:GetMouseLocation().X, minX, maxX)

    local mouseY = math.clamp(UserInputService:GetMouseLocation().Y, minY, maxY)

    local xPixel, yPixel = (maxX - minX), (maxY - minY)

    local xPos = (mouseX - minX) / xPixel

    local yPos = (mouseY - minY) / xPixel

    local scale = UDim2.new(xPos, 0, yPos, 0)


    local newLo = location:Clone()

    table.insert(locations, newLo)

    newLo.Visible = true

    newLo.Position = scale

    newLo.Parent = location.Parent

    local anotherLo = newLo:Clone()

    anotherLo.Visible = true

    anotherLo.Size = UDim2.new(0.116, 0,0.111, 0)

    anotherLo.Position = scale

    anotherLo.Parent = GPS_BUTTON.Parent.Parent.Frame.Map

    table.insert(locations, anotherLo)

    local Raycast = game.Workspace.CurrentCamera:ScreenPointToRay(newLo.AbsolutePosition.X, newLo.AbsolutePosition.Y, 0)

    local Ray = Ray.new(Raycast.Origin, Raycast.Direction * 100)

    local hit, pos = game.Workspace:FindPartOnRayWithIgnoreList(Ray, {char})

    local PART = Instance.new("Part")

    PART.Size = PART.Size + Vector3.new(0, 20, 0)

    pos = Vector3.new(pos.X, 10 , pos.Z)

    PART.Position = pos

    PART.Anchored = true

    PART.Parent = game.Workspace

    PART.CanCollide = false

    PART.Transparency = 0.5

    PART.Name = "GPS";
end)

and also a picture of how it works…

Also, I’m very sorry if you’ve seen this type of post multiple times. The thing is I just need help that can help me solve this. Thank you for reading my annoying posts. :metal:

1 Like

I see you have the player’s position on the minimap, so really all you need to do is reverse that process once you get where exactly on the map the player clicked. You’re over complicating the math a bit, so use something like this:

local Mouse = game.Players.LocalPlayer:GetMouse()
local Map = -- frame of the map

local relPos = Vector2.new(Mouse.X,Mouse.Y)-Map.AbsolutePosition -- relative position in pixels
local scalePos = relPos/Map.AbsoluteSize -- relative position in scale

Add some checks to make sure that this click is actually within the frame of the map (scalePos.X and scalePos.Y should both be positive and less than or equal to 1)

Once you have that position (either in pixels or scale, however you’re doing it), just reverse the process you did for placing the player on the map and get a position in the X/Z plane in the 3D space. Then from that position at some high Y coordinate raycast downwards to find the ground and place your physical waypoint.

1 Like

I don’t really understand the process after getting the scale position… Mind explaining a bit more about it?

Well I’m not sure how you placed the player’s position marker on the map which is why I was vague. I imagine you’re doing that by taking the player’s X/Z position and doing some math to place a marker in the proper relative spot. So for the waypoint, once you have the 2D position, just reverse that process to get it’s relative 3D position.

It’s like how converting Fahrenheit to Celsius is (F - 32) * (5/9) and Celsius to Fahrenheit is C * (9/5) + 32. It’s really the same thing, but the process is reversed.

I have two types of maps in my game, one is what I call a “large map” where it shows the whole map, and I just move the direction arrow there, then there’s a minimap where I move the map image. Will I do something like this?

local position = Map.AbsoluteSize/relPos