In my game, I have parts that are supposed to be spun around infinitely, usually with while true do.
While working on it, a question struck me, which is more performance friendly? A unanchored brick that is welded to a brick that spins but is also anchored or just using PrimaryPartCFrame on the point that spins with the part attached to it while both are Anchored?
I’ve heard that SetPrimaryPartCFrame has floating point errors if used for a long time which could cause the part to move away a bit but I’d also prefer to stay away from unanchored bricks which could cause lag via physics.
Which one should I use? (Note, I’m using a local script and want to make the part spin in 360 degrees)
Code I’m using with unanchored bricks:
while true do
wait()
part.CFrame = part.CFrame*CFrame.Angles(0,-0.1,0)
end
More Context On the setup:
The way EgoMoose rotated models for his playground testing place for his gravity controller was brilliant for making sure parts stay within a certain distance away from the primary part. This was done using a cached table that stored the relationship of each part’s CFrame to the PrimaryPart's CFrame, and rotating around the PrimaryPart.
Here is the code he used for it:
local function cacheOffset(model, cache, center)
local cache = cache or {};
local center = center or model.PrimaryPart.CFrame;
for _, child in next, model:GetDescendants() do
if (child:IsA("BasePart")) then
cache[child] = center:inverse() * child.CFrame;
end
end
return cache;
end
local function transformModel(model, cf, cache, center)
local center = center or model.PrimaryPart.CFrame;
for _, child in next, model:GetDescendants() do
if (child:IsA("BasePart")) then
child.CFrame = cf * cache[child];
end
end
end
local model = script.Parent.Parent;
local cache = cacheOffset(model);
game:GetService("RunService").Heartbeat:Connect(function(dt)
transformModel(model, model.PrimaryPart.CFrame*CFrame.Angles(0, math.rad(1)*dt*60, 0), cache);
end)
Is it efficient, I don’t know. Is it interesting, that it is.
3 Likes
Decided to use welds instead and CFrame the part as I find it easier and more efficient in my case.
1 Like