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

How does one rotate a Model using a script?

Asked by
Xduel 211 Moderation Voter
10 years ago

Let's say I am displaying a model, and I want the whole model to rotate slowly on an axis. Or in another case, I am trying to make an animated creature in which it's arms/legs move as a model. Is there any algorithm or function I can use to achieve this? Help would be appreciated, thanks you.

  • Xduel

1 answer

Log in to vote
0
Answered by
MrNicNac 855 Moderation Voter
10 years ago

CFraming a model is no problem. Besides, the idea is simple, isn't it. The big issue is getting the right relation. What I mean by this is, you have to have to some base to how you want it rotated - what to rotate based on (or change the CFrame at all).

What I've done is make this function get all the parts in a model (descendants too) and pick the first part in the table to use as the "basis." What happens is that all the CFrames are saved in relation to this "base" part. It uses the difference between angle/position. So if one part was 3 studs to the right, and 30 degrees angled in respect to the main part, then it will stay that way when the main part is moved/rotated. So, obviously, if the parts always stay the same in respect to the main part - they will seem to "rotate/move as a model."

function GetAllPartsInModel(M)
    local AllParts = {}
    local function Grab(Ancestor)
        for i,v in pairs(Ancestor:GetChildren()) do
            if (#v:GetChildren() > 0) then
                Grab(v)
            end
            if v:IsA("BasePart") then
                table.insert(AllParts, v)
            end
        end
    end
    Grab(M);
    return AllParts;
end

function SetModelCFrame(Model, NewCFrame)
    local Parts = GetAllPartsInModel(Model);
    local CFrames = {};
    local MainPart = Parts[1]
    for i,v in pairs(Parts) do
        if i > 1 then
            CFrames[v] = MainPart.CFrame:toObjectSpace(v.CFrame)
        end
    end
    MainPart.CFrame = NewCFrame
    for i,v in pairs(Parts) do
        if i > 1 then
            v.CFrame = MainPart.CFrame:toWorldSpace(CFrames[v])
        end
    end
end

SetModelCFrame(Workspace.Model, CFrame.new(0,10,0) * CFrame.Angles(0,1.5,0))
0
Thank you so much. I think I'll spend a couple hours researching CFraming :-) Xduel 211 — 10y
Ad

Answer this question