Help With Converting vector 2 to udim2

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Hello I want to make the cursor lock on a target when on-screen but I cursor is placed in the wrong place

  2. What is the issue? Include screenshots / videos if possible!


    OffPlace2

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

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.fromOffset(screenpos.X, screenpos.Y)
	else
		local MouseLocation = userinput:GetMouseLocation()
		TargetCursorGui.TargetCursor.Position = UDim2.fromOffset(MouseLocation.X, MouseLocation.Y)
	end
end)
1 Like

You don’t need to create a Vector2 value, just use screenpos.X and screenpos.Y they describe the offset from the top left hand corner of the screen in pixels to the point in world space, Z describes the depth.

1 Like

so how do i fix the issue where the cursor is in the wrong place?

Is the icon sized by scale or offset?
If offset then this should work.

TargetCursorGui.TargetCursor.Position = UDim2.fromOffset(screenpos.X-TargetCursorGui.TargetCursor.Size.X.Offset/2, screenpos.Y-TargetCursorGui.TargetCursor.Size.Y.Offset/2)

the GUI size is on scale do I need to convert it to offset?

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)

ok, the target works but how about the cursor?
asda

Are you trying to remove the cursor?

no, i want the target cursor to be in the middle of the mouse cursor

What’s the current size of the aim icon?

the aim icon size is at {0.075, 0},{0.17, 0}

That’s why it’s off-center, it should be the same size in both axis.

it’s didn’t work

TargetCursorGui.TargetCursor.Position = UDim2.new(TargetCursorGui.Size.X.Scale/2, MouseLocation.X+TargetCursorGui.Size.X.Offset/2, TargetCursorGui.Size.Y/Scale.2, MouseLocation.Y+TargetCursorGui.Size.Y.Offset/2)

Apologies, I see what you were referring to as your issue, change it back to the previous size.

1 Like

so as we know Vector2 is udim2 offset state ok? so if you want scale we need to know what scale even is

scale is the percentage of the screen and object so what you can do is get the screens size using

Mouse.ViewSizeX or Mouse.ViewSizeY

then divide it by the Vector2 to get the position in scale

Rewriting this post, one moment.

wait on line 18 it says TargetCursorGui.Size

Rewriting this post, one moment.

Rewriting this post, one moment.

Rewriting this post, one moment.