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

Resizing a model or a part changes the PivotPoint in-game?

Asked by 1 year ago

So anyways I have a script where players can resize their model

now the problem I have is that because resizing changes the pivot point

whenever I use :GetPivot() onto their model whenever they drag

the pivot changes so basically if they resize like 5 upwards on the y axis

everytime they try to pivot it its like 5 studs upwards on the y axis

so I was wondering if there was a way to stop this from happening

and No I do not want any answers that make me use SetPrimaryPartCFrame or all the other deprecated functions

1 answer

Log in to vote
1
Answered by 1 year ago
Edited 1 year ago

I'm assuming you're using my answer from the previous question. Sadly, you can't change model's pivot offset. However, if you want to get the original pivot, you can create the attributes OriginalPosition, OriginalXDirection, OriginalYDirection, and OriginalZDirection in the model since a pivot (aka CFrame) has a position and a direction. To get the CFrame from position and direction, use CFrame.fromMatrix(position, xDirection, yDirection, zDirection).

function ScaleModel(model: Model, xScale: number, yScale: number, zScale: number): Model
    local cf, _ = model:GetBoundingBox()
    local origin = cf.Position

    local OriginalPosition = model:GetAttribute("OriginalPosition")
    if OriginalPosition == nil then
        model:GetAttribute("OriginalPosition", origin)
        OriginalPosition = model:GetAttribute("OriginalPosition")
    end
    local OriginalXDirection = model:GetAttribute("OriginalXDirection")
    if OriginalXDirection == nil then
        model:GetAttribute("OriginalXDirection", cf.XVector)
        OriginalXDirection = model:GetAttribute("OriginalXDirection")
    end
    local OriginalYDirection = model:GetAttribute("OriginalYDirection")
    if OriginalYDirection == nil then
        model:GetAttribute("OriginalYDirection", cf.YVector)
        OriginalYDirection = model:GetAttribute("OriginalYDirection")
    end
    local OriginalZDirection = model:GetAttribute("OriginalZDirection")
    if OriginalZDirection == nil then
        model:GetAttribute("OriginalZDirection", cf.ZVector)
        OriginalZDirection = model:GetAttribute("OriginalZDirection")
    end

    local originalCFrame = CFrame.fromMatrix(OriginalPosition, OriginalXDirection, OriginalYDirection, OriginalZDirection)

    local scale = Vector3.new(xScale, yScale, zScale)

    for _, obj in pairs(model:GetDescendants()) do
        local OriginalSize = obj:GetAttribute("OriginalSize")
        if OriginalSize == nil then
            if obj:IsA("BasePart") then
                obj:GetAttribute("OriginalSize", obj.Size)
            elseif obj:IsA("JointInstance") then
                obj:GetAttribute("OriginalSize", Vector.zero)
            elseif obj:IsA("SpecialMesh") then
                obj:GetAttribute("OriginalSize", obj.Scale)
            end
            OriginalSize = obj:GetAttribute("OriginalSize")
        end

        local DontScale = obj:GetAttribute("DontScale")
        if DontScale == nil then
            if obj:IsA("BasePart") or obj:IsA("SpecialMesh") then
                obj:SetAttribute("DontScale", false)
                DontScale = obj:GetAttribute("DontScale")
            elseif obj:IsA("JointInstance") then
                DontScale = false
            else
                DontScale = true
            end
        end

        if DontScale == false then
            if obj:IsA("BasePart") then
                local newSize = OriginalSize * scale
                obj.Size = newSize

                local distance = (obj.Position - origin)
                local rotation = (obj.CFrame - obj.Position)
                obj.CFrame = (CFrame.new(origin + distance*scale) * rotation)
            elseif obj:IsA("JointInstance") then
                local c0NewPos = obj.C0.p*scale
                local c0RotX, c0RotY, c0RotZ = obj.C0:ToEulerAnglesXYZ()

                local c1NewPos = obj.C1.p*scale
                local c1RotX, c1RotY, c1RotZ = obj.C1:ToEulerAnglesXYZ()

                obj.C0 = CFrame.new(c0NewPos)*CFrame.Angles(c0RotX, c0RotY, c0RotZ)
                obj.C1 = CFrame.new(c1NewPos)*CFrame.Angles(c1RotX, c1RotY, c1RotZ)
            elseif obj:IsA("SpecialMesh") then
                local newScale = OriginalSize * scale
                obj.Scale = newScale
            end
        end
    end
    return model
end
0
I figured it out, all I had to do was reset the PivotPoint back to the center every single time I moved it but anyways this works too thanks for answering my questions. Overseer_Prince 188 — 1y
0
Also the Model:ScaleTo() function you had mentioned, I have checked it out and I think that functions scales models on 3 axis and it does not give you any options to resize on a single axis. I know this because theres only one parameter that accepts a number so I shouldn't be that excited for that function Overseer_Prince 188 — 1y
0
It's not like that. If you want to scale a model to Vector3.new(50, 50, 50) you can do model:ScaleTo(50) T3_MasterGamer 2189 — 1y
0
And also, you said that "whenever the model scales 5 upwards y axis, pivot also moves 5 upwards y axis" so i thought you were trying to get the previous pivot T3_MasterGamer 2189 — 1y
Ad

Answer this question