How to place a frame at a parts position

I’m trying to figure out how I could visualize hitboxes (place a frame at a parts location) using frames I feel like that would be nicer them using parts I’m not exactly sure how to do this though I’m assuming I’d probably need to WorldToViewportPoint but I’m not sure how to use it to position a frame lol.

Here’s my code for making the hitboxes

local function MakePart(Size,Position,Rotation,Name,FD)
	--| This part is the REAL hitbox that will make contact with the opponent
	local Part = Instance.new("Part")
	Part.CastShadow = false
	Part.Name = Name
	Part.Size = Size
	Part.Position = Position + Vector3.new(0,0.5,3)
	Part.CanCollide = false
	Part.Anchored = false
	Part.Color = Colors[Name]
	Part.Material = Enum.Material.Neon
	Part.Transparency = 1
	if not Rotation then
		Part.Orientation = Vector3.new(0,90,0)
	else
		Part.Orientation = Vector3.new(math.abs(Rotation[1])*FD,Rotation[2],Rotation[3])		
	end
	
	local Weld = Instance.new("WeldConstraint")
	Weld.Parent = Part
	Weld.Part0 = Part
	Weld.Part1 = Main.Character.HumanoidRootPart
	
	Part.Parent = Main.Character
	
	--local Camera = workspace.Camera
	--local Vector,OnScreen = Camera:WorldToScreenPoint(Part.Position)
	--local Frame = Instance.new("Frame")
	--Frame.Parent = Main.ScreenGui
	
	if ShowHitboxes.Value == true then
		--| Fake hitbox which is a flat version of the real one for visualization
		VisualHitbox = Instance.new("Part")
		VisualHitbox.Name = "VisualHitbox"
		VisualHitbox.Size = Vector3.new(0,Part.Size.Y,Part.Size.Z)
		VisualHitbox.Orientation = Vector3.new(0,90,0)
		VisualHitbox.CFrame = Part.CFrame + Vector3.new(0,0,1.3)
		VisualHitbox.Color = Colors[Name]
		VisualHitbox.Transparency = 0.5	
		local Weld2 = Instance.new("WeldConstraint")
		Weld2.Parent = VisualHitbox
		Weld2.Part0 = VisualHitbox
		Weld2.Part1 = Main.Character.HumanoidRootPart
		VisualHitbox.CanCollide = false
		VisualHitbox.Parent = Main.Character
		table.insert(VisualHitboxs,VisualHitbox)
	end

	--| Outline
	local Outline = Instance.new("SelectionBox")
	if ShowHitboxes.Value == true then
		Outline.Transparency = 0
		Outline.Adornee = VisualHitbox
		Outline.Color3 = Colors[Name]
		Outline.Parent = VisualHitbox
		Outline.LineThickness = 0.03
	end

	return Part
end

2 Likes

After digging around a bit I found this code from a devforum post

function WorldToScreen(Pos) --This function gets a World Position (Pos) and returns a Vector2 value of the screen coordinates
	local point = Cam.CoordinateFrame:pointToObjectSpace(Pos)
	local aspectRatio = M.ViewSizeX / M.ViewSizeY
	local hfactor = math.tan(math.rad(Cam.FieldOfView) / 2)
	local wfactor = aspectRatio*hfactor

	local x = (point.x/point.z) / -wfactor
	local y = (point.y/point.z) /  hfactor

	return Vector2.new(M.ViewSizeX * (0.5 + 0.5 * x), M.ViewSizeY * (0.5 + 0.5 * y))
end

And then all I had to do was

	if ShowHitboxes.Value == true then
		local Vec2 = WorldToScreen(Part.Position)
		local Frame = Instance.new("Frame")
		Frame.Size = UDim2.new(0.1,0,0.1,0)
		Frame.Position = UDim2.new(0,Vec2.X-90,0,Vec2.Y+10)
		Frame.BackgroundTransparency = 0.5
		Frame.BackgroundColor3 = Colors[Name]
		Frame.Parent = Main.ScreenGui
		table.insert(VisualHitboxs,Frame)
	end

Seems to work fine

1 Like