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)
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.