How to get the Y value of a model's size

  1. What do you want to achieve?

Im trying to get the global Y size of a model (Basically the y value of the highest point in the model subtracted by the y value of the lowest point in the model).

  1. What is the issue? Include enough details if possible!

Im not sure how I would go about solving this. Getting the Y value of model:GetBoundingBox() wouldn’t work since the bounding box could be rotated, making the Y value innacurate.

  1. What solutions have you thought of so far?

Ive done some research but couldn’t find any solutions. The solutions that I did find seem to be innacurate as well.

Has also print statement to show height model info

local function getModelHeight(model)
    local highestPoint = -math.huge
    local lowestPoint = math.huge

    for _, part in ipairs(model:GetDescendants()) do
        if part:IsA("BasePart") then
            local size = part.Size
            local cframe = part.CFrame

            -- Calculate the corners of the part
            local corners = {
                cframe * Vector3.new(size.X / 2, size.Y / 2, size.Z / 2),
                cframe * Vector3.new(size.X / 2, size.Y / 2, -size.Z / 2),
                cframe * Vector3.new(size.X / 2, -size.Y / 2, size.Z / 2),
                cframe * Vector3.new(size.X / 2, -size.Y / 2, -size.Z / 2),
                cframe * Vector3.new(-size.X / 2, size.Y / 2, size.Z / 2),
                cframe * Vector3.new(-size.X / 2, size.Y / 2, -size.Z / 2),
                cframe * Vector3.new(-size.X / 2, -size.Y / 2, size.Z / 2),
                cframe * Vector3.new(-size.X / 2, -size.Y / 2, -size.Z / 2)
            }

            -- Check each corner to find the highest and lowest points
            for _, corner in ipairs(corners) do
                if corner.Y > highestPoint then
                    highestPoint = corner.Y
                end
                if corner.Y < lowestPoint then
                    lowestPoint = corner.Y
                end
            end
        end
    end

    -- Calculate the height
    local height = highestPoint - lowestPoint
    return height
end

-- Example usage:
local model = workspace.YourModel -- Replace with your model's path
local height = getModelHeight(model)
print("The height of the model is: " .. height

Not sure why I didn’t think of just manually finding it for each part. Thanks, Ill give it a try

Although this has been solved, there is a method that I think is worth pointing out.
GetExtentsSize will return the ExtentsSize of the model. If the model has a primary part, then it will be aligned with that primary part. So if we have a primary part, with no rotation, the extents size will be calculated without rotation. Avoiding the problem of if the model is rotated.

function GetGlobalHeight(model: Model): number
	local PrimaryPart = model.PrimaryPart
	local Center = Instance.new("Part") -- has no rotation by default
	
	Center.Position = model:GetBoundingBox().Position
	model.PrimaryPart = Center
	
	local Y = model:GetExtentsSize().Y -- returns size of smallest bounding box containing all model components **aligned with primary part** if it exists
	model.PrimaryPart = PrimaryPart
	
	Center:Destroy()
	return Y
end
print(GetGlobalHeight(YourModel))

The only potential issue is the temporary swapping out of the primary part, which you will need to test whether to see if it interferes with any scripting or structures dependent on it.

1 Like

Oh i knew that extents size would rotate with the model but I didnt know that it aligns with a primary part if it is present. Thanks for the input

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.