How to convert vector3 to udim2?

Hi,
I have problem, that I need to convert vector3 to udim2 on players screen (so make pointer to the vector), is this possible? (as when i have mouse.Hit, but reversed)

4 Likes

Its not possible to convert, as Vector3 gives a position in a 3D world while Udim2 gives a position or size in a 2D world.

1 Like

ik, but i need to make pointer on players gui to vector3

1 Like

Position = UDim 2 . new( 0 , Vec 2. X, 0 , Vec 2. Y) --Since the function doesn’t return a UDim2 value, you have to create a Udim2 value and put the X and Y of the Vector2 into that UDim2 .

You cant make a 2D object to a 3D object as it has no 3rd dimension. The other way around you need to delete a dimension.

1 Like

if you mean by a gui that follows the mouse pointer, then this may help(you didn’t explain very well):

local player = game.Players.LocalPlayer
local RS = game:GetService("RunService")

RS.RenderStepped:Connect(function()
    script.Parent.Position = UDim2.new(0, mouse.X, 0, mouse.Y)
end)
6 Likes

Stravant has a ScreenSpace module script which contains multiple functions for interactions between the 2D screen and the 3D environment. I’ll give you the function from the module that deals with what you’re asking for:

local M = game.Players.LocalPlayer:GetMouse()
local Cam = game.Workspace.CurrentCamera

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

local Vec2 = WorldToScreen(Vector3.new(0, 50, 0)) --Replace Vector3.new(0, 50, 0) with whatever vector3/position you want

Gui.Position = UDim2.new(0, Vec2.X, 0, Vec2.Y) --Since the function doesn't return a UDim2 value, you have to create a Udim2 value and put the X and Y of the Vector2 into that UDim2.

Hope this helped!

Note: This only works if it’s in a localscript. Also, the Vector2 will be the coordinates of the point from the top left corner of the screen, so if the gui is located in another gui that’s offset from the (0, 0), the gui will be positioned incorrectly.

29 Likes

The camera provides a function to take a Vector3 and turn it into a Vector3 which could be changed into a UDim2. (x represents x offset, and y represents y offset)

6 Likes

@ZexityBlox and @Halalaluyafail3, and what x and y will it return, when it will be out of screen

1 Like

Try this?

1 Like

bro this isn’t very good for your game performance…

Is there a way to make this compatible with AnchorPoint(0, 0) instead of 0.5, 0.5? Im currently working on a blood dripping UI effect, and i have to resize it with TweenService, id have to reposition it too if the AnchorPoint is 0.5, 0.5