Hi! I’m working on building tools similar to the classic ones (move, clone, delete) and I need a way to ‘safe move’ parts out of other intersecting parts. This behaviour if I remember correctly is available through setting Position directly, but that’s not very future proof as there has been intention of removing that feature, so I would like a Lua implementation.
My current code does not work correctly, and is approximate at best:
local function moveUpUntilFree(part)
part:BreakJoints()
local wasAnchored = part.Anchored
part.Anchored = true
while #part:GetTouchingParts() > 0 do
part.CFrame = part.CFrame + Vector3.new(0, 0.1, 0)
end
part.Anchored = wasAnchored
end
The ideal solution would:
preserve properties like Anchored
provide a more exact position for the part, rather than this kind of rough approximation
work with all kinds of parts and part shapes
What would be the best way of implementing such a function?
The removal of safe move is unlikely to happen as many games likely rely on that behavior and would break. If it were to be removed, an alternative would likely be provided, or it would be implemented such that it would not break existing games.
You shouldn’t worry about this if future proofing is your only reason for implementing this.
Oh.
Nevertheless, consider raycasting straight down from above the part using the set of parts returned from GetTouchingParts as a whitelist. Then you can get the intersection point of the highest true surface and move the part there + it’s height offset (raycast up from under the part to determine this?).
I just ran into this case a few days ago, actually! Funny timing.
--Move this part up until it intersects with nothing.
--Funny little hack. :)
local oldParent = part.Parent;
local m = Instance.new("Model");
part.Parent = m;
m.PrimaryPart = part;
m.Parent = workspace;
m:MoveTo(from.p);
part.Parent = oldParent;
m:Destroy();