Help With Converting vector 2 to udim2

nope it didn’t worksadd

local TargetCursorGui = script.TargetCursorGui
local userinput = game:GetService("UserInputService")
local camera = workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local targetPart = workspace:WaitForChild("Target")
local RunService = game:GetService("RunService")
TargetCursorGui.Parent = plr.PlayerGui
RunService.Stepped:Connect(function()
	local screenpos, Onscreen = camera:WorldToScreenPoint(targetPart.Position)
	if Onscreen then
		local screenposVector2 = Vector2.new(screenpos.X, screenpos.Y)
		print("OnScreen")
		print(screenposVector2)
		TargetCursorGui.TargetCursor.Position = UDim2.new(-TargetCursorGui.TargetCursor.Size.X.Scale/2, screenpos.X-TargetCursorGui.TargetCursor.Size.X.Offset/2, -TargetCursorGui.TargetCursor.Size.Y.Scale/2, screenpos.Y-TargetCursorGui.TargetCursor.Size.Y.Offset/2)
	else
		local MouseLocation = userinput:GetMouseLocation()
		TargetCursorGui.TargetCursor.Position = UDim2.new(-TargetCursorGui.TargetCursor.Size.X.Scale/2, MouseLocation.X-TargetCursorGui.TargetCursor.Size.X.Offset/2, -TargetCursorGui.TargetCursor.Size.Y.Scale/2, MouseLocation.Y-TargetCursorGui.TargetCursor.Size.Y.Offset/2)
	end
end)

I should go to bed, forgot the minus signs in front.

1 Like

Don’t forget about AnchorPoint!!! Setting the AnchorPoint property of the TargetCursor instance to (0.5,0.5) causes the position to be based off the center of the UI!!
Eliminates the need for you to set the scale parameters to anything other than 0

local TargetCursorGui = script.TargetCursorGui
local camera = workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local targetPart = workspace:WaitForChild("Target")
local RunService = game:GetService("RunService")
TargetCursorGui.Parent = plr.PlayerGui
RunService.RenderStepped:Connect(function()
	local screenpos, Onscreen = camera:WorldToScreenPoint(targetPart.Position)
	if Onscreen then
		print("OnScreen")
		TargetCursorGui.TargetCursor.Position = UDim2.new(0, screenpos.X, 0, screenpos.Y)
	else
		local Mouse = plr:GetMouse()
		TargetCursorGui.TargetCursor.Position = UDim2.new(0, Mouse.X, 0, Mouse.Y)
	end
end)

ut was that easy? i spent more than 1hour figuring this out and all I had to do is to set the anchor point bruh -_- but thank you a lot

Check the message I sent you, thanks.

I avoided using AnchorPoint intentionally, in-case in future it was desired that the scope icon should float around the cursor (smoothly) as often observed in various FPS games.