Show waypoint on minimap

I need help displaying marks on a minimap. It ALWAYS gets the position incorrect. I worked on this for like 2 weeks. It basically uses raycasts to get terrain colors and puts it in a viewportframe.

local surface = script.Parent.Parent
local terrain = workspace.Terrain
local cam = workspace.CurrentCamera
local mapFrame = script.Parent:WaitForChild('Map')
local markerFolder = mapFrame:WaitForChild('Marks')
local workspaceMarkers = workspace:WaitForChild('MapMarked')
local mapCam = Instance.new('Camera',workspace)
mapCam.Name=tostring({})
mapFrame.CurrentCamera=mapCam
local lp = game:GetService('Players').LocalPlayer
local tool = lp:WaitForChild('Backpack'):WaitForChild('GPS')
surface.Adornee=tool:WaitForChild('Screen')
local playerDisplay = mapFrame:WaitForChild('Player')
local partFolder = mapFrame:WaitForChild('Parts')
local char = lp.Character or lp.CharacterAdded:Wait()
local hroot = char.HumanoidRootPart
local prevparts = {}
local M = lp:GetMouse()
local Cam = mapCam
Cam.CameraType=Enum.CameraType.Scriptable
Cam.FieldOfView = 70
local scale = 0.3
local direction = Vector3.new(0, -150, 0)
local raycastParams = RaycastParams.new()		
local camY = 300
local WorldToScreen = function(pos,obj)
	local tempCam = Instance.new('Camera')
	tempCam.CameraType=Enum.CameraType.Scriptable
	tempCam.CFrame=(
		cam.CFrame-
		Cam.CFrame)
	local pos,vis = tempCam:WorldToScreenPoint(pos)
	print(pos)
	local x = pos.X-- - obj.AbsoluteSize.X/2
	local y = pos.Y-- - obj.AbsoluteSize.Y/2
	tempCam:Destroy()
	return UDim2.new(0,x,0,y)
end
local pos = terrain.Position + Vector3.new(0, 20, 0)
camY-=pos.Y
local function makePart()
	local prt = Instance.new('Part', partFolder)
	prt.CanCollide = false
	prt.Anchored = true
	prt.Position = Vector3.new(0, 0, 0)
	prt.Size = Vector3.new(1 * scale, 1 * scale, 1 * scale)
	return prt
end
local function makeMark(bgc,tc,t)
	local mark = markerFolder:WaitForChild('Sample'):Clone()
	mark.Parent=markerFolder
	mark.Icon.BackgroundColor3=bgc
	mark.Icon.TextLabel.Text=t
	mark.Icon.TextLabel.BackgroundColor3=tc
	mark.Visible=true
	return mark
end
local partsPerCast = terrain.Size.X*(1+scale)
spawn(function()
	while task.wait() do
		playerDisplay.Rotation=-hroot.Orientation.Y-90
	end
end)
function runRay(i,posMod)
	for i2 = 0, (partsPerCast) do
		local ray = workspace:Raycast(posMod(i2), direction, raycastParams)
		if ray then
			local part = makePart()
			part.Color = Color3.fromRGB(255, 255, 255)
			part.Position = posMod(i2)
			local hit = ray.Instance
			if hit then
				local _,_e =	pcall(function()
					part.Color = workspace.Terrain:GetMaterialColor(ray.Material)
				end)
				if _e then
					part.Color = Color3.fromRGB(0, 85, 255)
				end
			end
		else 
			--break
		end
	end
end
local shouldWait = 0

spawn(function()
	while task.wait() do
		local cf = CFrame.new(
			Vector3.new(hroot.Position.X,camY,hroot.Position.Z),
			Vector3.new(0,-90,0)
		)
		mapCam.CFrame=cf
		local ud2 = WorldToScreen(hroot.Position,playerDisplay)
		playerDisplay.Position=ud2
	end
end)
for i,v in pairs(workspaceMarkers:GetChildren()) do
	local _settings = v:WaitForChild('Settings')
	local display = makeMark(_settings.IconBgColor.Value,_settings.IconTextColor.Value,_settings.IconText.Value)
	local visible = _settings.Visible.Value
	spawn(function()
		while task.wait() do
			display.Visible = _settings
			local ud2 = WorldToScreen(v.Position,display)
			display.Position=ud2
		end
	end)
end
for i = 1, partsPerCast*2 do
	runRay(i,function(i2)
		return pos + Vector3.new(i * scale, 0, i2 * scale)
	end)
	runRay(i,function(i2)
		return pos - Vector3.new(i * scale, 0, i2 * scale)
	end)
	runRay(i,function(i2)
		return pos + Vector3.new(3-i * scale, 0, i2 * scale)
	end)
	runRay(i,function(i2)
		return pos - Vector3.new(3-i * scale, 0, i2 * scale)
	end)
	if shouldWait==15 then
		task.wait()
		shouldWait=0
	else
		task.wait()
		shouldWait+=1
	end
end
1 Like