-Introduction-
Hello everyone! I noticed that there are posts about the destruction of windows, but no open sourced code or modules I could find on the subject. So, I created FragmentSmasher, a simple module with one function for the destruction of windows and other things.
The module contains one useful function,BreakSurface(), for breaking apart a square/rectangular shaped part. It works by taking the original part, and replacing it with a multitude of triangles of the same total size.
-Examples-
BreakSurface():
FragmentSmasher= require(game.ServerStorage.FragmentSmasher)
FragmentSmasher.BreakSurface(Pane,BreakOriginPositionVector,
DirectionalVelocityVector,
SoundId,
DestroyTime)
--[[
-Pane- the Part you want to effect
-BreakOriginPositionVector- the center of destruction, and the middle point of the calculations
-DirectionalVelocityVector- a direction Vector3 that determines the general direction for the fragments to fly toward(Pass the string "Default" to use the Default vector)
-SoundId- the SoundId to be played on smash(Pass the string "None" to use no Sound)
-DestroyTime- the amount of time that passes before the fragments are deleted from the workspace(Pass the string "Never" for the fragments to exist for a infinite amount of time)
]]--
Basic Usage:
Code:
FragmentSmasher= require(game.ServerStorage.FragmentSmasher)
FragmentSmasher.BreakSurface(workspace.WindowPanePart,workspace.WindowPanePart.Position,
"Default",
5183344180,
10)
This code is a basic example of how to break a window, or other object.
Outcome:
Gun Example:
Example Code:
FragmentSmasher = require(game.ServerStorage.FragmentSmasher)
local Gun = "gun"
MouseHit = "mouse hit position"
function OnShoot()
local ray = Ray.new(Gun.Barrel.Position, (MouseHit - Gun.Barrel.Position).unit * 300)
local part, position = workspace:FindPartOnRayWithIgnoreList(ray, {Gun})
if part.Name == "Window" then
FragmentSmasher.BreakSurface(
part,
position.Position,
Gun.Barrel.CFrame.LookVector * 75 + Vector3.new(0, 25, 0),
5183344180,
10
)
end
end
This basic example creates a Ray, which it then uses to find the position the Ray hit, and the part the Ray hit. If the part is named “Window”,it fires the module’s BreakSurface() function with a BreakOriginPositionVector of the Ray hit position, and a DirectionalVelocityVector that faces away from the shotgun. This results in something similar to the following I created.
Result:
Fun, right?
-Notice-
Keep in mind that the long sides of your window must be the X and Y axis, otherwise the calculations will not work properly.
Link to obtain:
Enjoy!