Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to convert a Udim2 to a Vector3?

Asked by
kepiblop 124
3 years ago

Here is what I'm doing I am trying to make a visualization of a mouse for a in game computer for my game. I got some progress where I got this GUI that I'm gonna make invisible with a certain position for the part. (The mouse is white cause I put a visualization mouse cause lightshot doesn't wanna capture it lol.)

So here is what I am trying to do

see that semi-transparent part?? I'm trying to make that part go above the gui.

Here is what I tried to do i got this one module that converts it, (by snorebear)

I tried using it, it worked but all it did was make the part go into the abyss.

Another attempt was using the Vector2 offset to make the part go to the place by using the Z and X to replace the X and Y but that was a flop too.

I seriously cant think of anything else, help would be really appreciated right now

(THE SCRIPTS IF NEEDED) UD2TOV3:

-- Created by snorebear :: 4/20/2019
-- Please provide credit with any modifications you make.

local API = {};

local function recurseUp(obj)
    if obj.Parent:IsA("ScreenGui") or obj.Parent:IsA("SurfaceGui") then
        return obj.Parent;
    end
    recurseUp(obj.Parent);
end

local offsetToScale = function(x,y,guiObj)
    x = x/recurseUp(guiObj).AbsoluteSize.x;
    y = y/recurseUp(guiObj).AbsoluteSize.y;
    return x,y;
end

local getNormal = function(part,guiObj)
    local x,y,z = part.CFrame.upVector, part.CFrame.rightVector, part.CFrame.lookVector;
    local x1,x2,y1,y2,z1,z2 = x,-x,y,-y,z,-z;

    local ct = {
        [Enum.NormalId.Front] = z1.z - part.Size.z;
        [Enum.NormalId.Back] = z2.z + part.Size.z;
        [Enum.NormalId.Right] = x1.x - part.Size.x;
        [Enum.NormalId.Left] = x2.x + part.Size.x;
        [Enum.NormalId.Top] = y.y - part.Size.y;
        [Enum.NormalId.Bottom] = y2.y + part.Size.y;
    };

    return ct[recurseUp(guiObj).Face];
end

local getPositionalValues = function(part,guiObj,type)
    local Scale = {};
    local x,y = offsetToScale(guiObj.Position.X.Offset, guiObj.Position.Y.Offset, guiObj);
    Scale.X = x;
    Scale.Y = y;
    local x1,y1,z1 = part.Size.X * Scale.X, part.Size.Y * Scale.Y, type == "cf" and getNormal(part,guiObj) or part.Size.Z;
    return Vector3.new(x1,y1,z1);
end

local toWorldSpace = function(part,guiObj,type)
    local pos = getPositionalValues(part,guiObj,type);
    local pos2 = Vector3.new(pos.x + part.Position.x + 0.5, pos.y + part.Position.y - 0.5, pos.z + part.Position.z + 0.5);
    return pos2;
end

local orConvert = function(part,guiObj)
    local x,y,z;
    x = part.Orientation.x;
    y = guiObj.Rotation + part.Orientation.y;
    z = part.Orientation.z;
    return x,y,z;
end

function API:convertUD2toV3(part,guiObj,type)
    if type == "v3" then
        return toWorldSpace(part,guiObj,type);
    elseif type == "cf" then
        local v3 = toWorldSpace(part,guiObj,type);
        local y,x,z = orConvert(part,guiObj);
        return CFrame.new(v3) * CFrame.fromEulerAnglesYXZ(math.rad(y),math.rad(x),math.rad(z));
    end
end

return API;

THE SCRIPT THAT MOVES THE GUI:

local uis = game:GetService("UserInputService"):GetMouseLocation() game.Workspace.SpawnLocation.SurfaceGui.Frame.Position = UDim2.fromOffset(uis.X,uis.Y) -- im using a spawn location cause i dont know i just wanted to get a replica place asap

ATTEMPT WITH MODULESCRIPT (which was a flop) [NOT PLAYTESTED JUST DONE ON STUDIO]

local module = require(game.Workspace.UD2toV3)
game.Workspace.Part.CFrame = module:ConvertUD2toV3(game.Workspace.Part,game.Workspace.SpawnLocation.SurfaceGui..Frame,"cf")

Thats all i can give you right now, any fixes would be highly appreciated and you'd be a life saver

I almost forgot! A replica place file is over here

0
alright easier solution is to not implement this feature and do random mouse movements instead or just use a 2d mouse. kepiblop 124 — 3y

Answer this question