I’m working on destructible buildings and windows in my game. I want to make an effect where once something is destroyed it will break into pieces. How can I do this along with being able to adjust how many pieces it breaks into?
Can you describe exactly the effect you are trying to achieve? What shape should the parts be, etc.? I think there are several implementations on the forums that do different things.
Heres exactly what I want to achieve. Lets say a player in my game shoots a window.
I want that window to be cut into X amount of pieces (just blocks) The amount of pieces will be determined by the size of the glass thats being broken. I would also like the size of these pieces to be randomized.
Is this what you are looking for
local Part = script.Parent
local function KillPC()
for I=1, Part.Size.X do
local SmallPart = Instance.new("Part", Part.Parent)
SmallPart.Material = Part.Material
SmallPart.Color = Part.Color
SmallPart.Size = Vector3.new(1, 1, 1)
end
end
Not quite. I would want the parts to be positioned to basically fill the glass. And the sizes to be random.
SmallPart.Position = Part.Position
i forgot to do that
Thats going to put every part in the middle. I’m trying to make them all fill the pane of glass pretty much.
You dont have to union it. You can create parts and position them.
Made this but I haven’t tried it. Let me know if you have any errors:
local function createPieces(part) -------- part is the window
if part ~= nil then
local size = part.Size
local position = part.Position
local pieces = (size.X+size.Y)/2
for i = 1, pieces do
local piece = Instance.new("Part", workspace)
piece.Name = part.Name.." piece"
piece.Size = Vector3.new(size.X/pieces, size.Y/pieces, size.Z)
piece.Position = Vector3.new(math.random(-posiion.X/1.25, position.X/1.25), math.random(-position.Y/1.25,position.Y/1.25), position.Z)
piece.Color = part.Color
piece.Transparency = part.Transparency
piece:ApplyImpulse(part.CFrame.LookVector * 50) ---- delete if you dont want the pieces to fling slightly
end
part:Destroy()
end
end
createPieces()
https://gyazo.com/6a0fbde0d9796a2249c7c9d99c1ff370
Here is the result.
I would also sometimes get this error if I hit one of the panes of glass that was angled differently.
invalid argument #2 to 'random' (interval is empty)
Fixed it. Put this in the part you want to break:
local function createPieces(part) -------- part is the window
if part ~= nil then
local size = part.Size
local position = part.Position
local pieces = (size.X+size.Y)/2
for i = 1, pieces do
local piece = Instance.new("Part", workspace)
piece.Name = part.Name.." piece"
piece.Size = Vector3.new((size.X/pieces)+math.random(0.25,1), (size.Y/pieces)+math.random(0.25,1), size.Z)
piece.CFrame = part.CFrame * CFrame.new(math.random(-size.X, size.X)/1.5, math.random(-size.Y,size.Y)/1.5, 0)
piece.Color = part.Color
piece.Transparency = part.Transparency
piece.Anchored = true
piece:ApplyImpulse(part.CFrame.LookVector * 50)---- delete if you dont want the pieces to fling slightly
end
part:Destroy()
end
end
createPieces(script.Parent)
You forgot to parent the piece
variable to something.
local piece = Instance.new(“Part”, workspace)
Oh my bad (I never really use the second argument haha), also you shouldn’t use the second argument, it has performance amplifications.
Here’s the post by the staff (If you’re interested):
Oh ok. I’ve used it for many years as a line saver lol. Thanks for the info!
It’s not exactly what you want, but I found these posts for breaking parts into triangles instead of blocks:
For blocks, the easiest option would be to pick a line to divide the part along a single axis, once. Then do the same for each of the two halves. Repeat until you have the number you’d like. Would be okay for most visual purposes and be easy to implement.
This is a promising approach for a slightly more random layout: geometry - Has anyone ever attempted to find all splits of a rectangle into smaller rectangles? - Mathematics Stack Exchange
o wow woke up to a lot of responses lol. I’m gonna try some of this stuff out. I’ll update you on how it goes.
Greatly appreciate the help.
I can think of 2 approaches to this.
The first one would basically be manually splitting up the windows and walls, and when they get broken, unanchor each piece and use bodyforces to make them fly out.
The second method would be actually scripting it to be shattered into customizable amounts of pieces. The first problem of this would how to actually do it; It’d definitely require a lot of math and thinking. The second problem with this is the shape of the pieces. If you had more than just cubes, say wedges, then it’d make things even more complicated.