I am trying to program destruction physics in a game.
My models consist of a bunch of cubes, which I want to be welded together since explosions break welds.
My issue is making the welds. I want to have each cube welded to the cubes that it is touching.
Manually making these welds would be tedious and take hours, though. How could I go about this? Is there any plugins anyone could recommend?
1 Like
simple enough, server script in serverscript service
local plane = workspace.Plane -- change the plane path
for _, v in ipairs(plane:GetDescendants()) do
if v:IsA("BasePart") then
v.Anchored = true
local touching = v:GetTouchingParts()
print(touching)
for _, touching_part in ipairs(touching) do
if touching_part:IsA("BasePart") then
touching_part.Anchored = true
local weld = Instance.new("Weld")
weld.Part0 = v
weld.Part1 = touching_part
weld.C0 = weld.Part0.CFrame:Inverse()
weld.C1 = weld.Part1.CFrame:Inverse()
weld.Name = string.format("%s->%s", v.Name, touching_part.Name)
weld.Parent = v
touching_part.Anchored = false
end
end
v.Anchored = false
end
end
NOTE: FOR THIS SCRIPT TO WORK, MAKE SURE THE PARTS ARE INTERSECTING, OTHERWISE IT WONT DETECT
If you dont want the parts to be intersecting but touching, you can do this:
local plane = workspace.Plane -- change the plane path
for _, v in ipairs(plane:GetDescendants()) do
if v:IsA("BasePart") then
v.Anchored = true
local region_stats = Region3.new(v.Position - v.Size / 2,v.Position + v.Size / 2) -- get part region stats
local touching = workspace:GetPartBoundsInBox(region_stats.CFrame, region_stats.Size)
print(touching)
for _, touching_part in ipairs(touching) do
if touching_part:IsA("BasePart") then
touching_part.Anchored = true
local weld = Instance.new("Weld")
weld.Part0 = v
weld.Part1 = touching_part
weld.C0 = weld.Part0.CFrame:Inverse()
weld.C1 = weld.Part1.CFrame:Inverse()
weld.Name = string.format("%s->%s", v.Name, touching_part.Name)
weld.Parent = v
touching_part.Anchored = false
end
end
v.Anchored = false
end
end
this script will weld parts that are touching together, regardless if they’re intersecting. If you know how to use module scripts then you should move this into a module script function.
@BucketProBro9
5 Likes