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

Retaining model rotation when moving via primary cframe?

Asked by
npott13 23
7 years ago

How can i retain the model rotation when moving model using SetPrimaryCFrame?

Because whenever i rotate the model by any degree and move it, the model don't retain the rotation i set. This is a problem i'm currently have with my dragging script...

The rotation script.

mouse.KeyDown:connect(function (key) -- Safe rotation
    pcall (function()
function rotateMod(mod,rotation)

    if key == "r" and _G.target:FindFirstChild("CanRotate") then 
local center = mod:GetModelCFrame()
    local parts ={}
    local function scan(parent)
        for _,obj in pairs(parent:GetChildren()) do
            if (obj:IsA("BasePart")) then
                table.insert(parts,obj)

            end
            scan(obj)
        end
    end
    scan(mod)
    for _,part in pairs(parts) do
        part.CFrame = (center*rotation*(center:inverse()*part.CFrame))
    end
end
end

rotateMod(_G.target,CFrame.Angles(0,math.rad(10),0))
end)
end)
0
stop using KeyDown :c OldPalHappy 1477 — 7y
0
the userinputservice didn't fixed it npott13 23 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

Instead of creating a new CFrame (which has no rotation) and moving the model to that, simply change its current CFrame by using GetPrimaryPartCFrame and simply adding the translation you would like. If you only know that you want to move the model to a specific location, then:

local model = workspace.MyModel --for example
local destination = Vector3.new(10, 5, 13) --for example
local curCFrame = model:GetPrimaryPartCFrame()
model:SetPrimaryPartCFrame(curCFrame - curCFrame.p + destination)

The - curCFrame.p gets rid of the cframe's position (setting it to 0, 0, 0); allowing + destination to set the position to exactly where you want while retaining the original rotation.

0
But i don't have specific destination... npott13 23 — 7y
Ad

Answer this question