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

How would I get the position at the exact center of a model?

Asked by 10 years ago

Not really much else to say. I've tried the following method-

--returns a list of positions
function PopulateList(model)

    local tbl = {
        x = {},

        y = {},

        z = {},

    }

    for _,v in pairs(model:GetChildren()) do
        tbl.x[#tbl.x + 1] = v.Position.X + (v.Size.X / 2)
        tbl.y[#tbl.y + 1] = v.Position.y + (v.Size.y / 2)
        tbl.z[#tbl.z + 1] = v.Position.z + (v.Size.z / 2)

    end

    return tbl

end

--returns the corners of the object in question
function SortGL(tbl)

    local gx, gy, gz, lx, ly, lz = 0, 0, 0, 1000, 1000, 1000
    local index = 1

    for _,v in pairs(tbl) do
        for _,pos in pairs(v) do
            if index == 1 then
                if pos > gx then
                    gx = pos
                elseif pos < lx then
                    lx = pos
                end
            elseif index == 2 then
                if pos > gy then
                    gy = pos
                elseif pos < ly then
                    ly = pos
                end
            elseif index == 3 then
                if pos > gz then
                    gz = pos
                elseif pos < lz then
                    lz = pos
                end
            end
        end
        index = index + 1
    end

    gpos = Vector3.new(gx, gy, gz)
    lpos = Vector3.new(lx, ly, lz)

    return gpos, lpos

end

function GenerateHitbox(obj)

    local gpos, lpos = SortGL(PopulateList(obj))

    local hitbox = Instance.new("Part", obj)
    hitbox.Name = "Hitbox"
    hitbox.Transparency = 0
    hitbox.Position = gpos

end

... and the result isn't anything like what I wanted (the position is very far off). How would I get the exact center of a model?

1
One thing I use to get a Model's CFrame is 'Model:GetModelCFrame()', it gets the CFrame of a Model [While also containing parts]. TheeDeathCaster 2368 — 10y
0
It worked! Thank you. I didn't see this when I looked on the wiki page, for some reason. Still, I'm wondering what exactly IS wrong with my script up top. Hm. IREaPAR110 5 — 10y

1 answer

Log in to vote
0
Answered by
Tkdriverx 514 Moderation Voter
10 years ago

Simply use model:GetModelCFrame().p to get the position matrix of the CFrame, but if you want the rotation matrix included, just use model:GetModelCFrame()

Ad

Answer this question