My intention was to create cracks using parts. For some reason, the cracks don’t go in the same direction.
Here is my code:
local origin = CFrame.new(0, 10, 0)
local mainBranchCount = math.random(8, 12)
local branches = {}
local possibleMultipliers = {-1, 1}
local rayParams = RaycastParams.new()
local whitelistParts = {}
local toWhitelist = {}
local branchParts = {}
for count = 1, mainBranchCount do
local multiplier = math.random(-1, 1)
if multiplier == 0 then
multiplier = multiplier - 1
end
local seperation = 360 / mainBranchCount
local caseSeperation = (count * seperation) - 180
local branchLength = math.random(2, 4)
local branchPosition = origin:ToWorldSpace(CFrame.Angles(0, 0, caseSeperation)):ToWorldSpace(CFrame.new(branchLength, 0, 0))
local branchPart = Instance.new("Part", workspace)
branchPart.Name = "branch"..tostring(count)
branchPart.Material = "SmoothPlastic"
branchPart.Color = Color3.new(0, 0, 0)
branchPart.Anchored = true
branchPart.Material = "Neon"
branchPart.Transparency = 0.5
branchPart.CanCollide = false
branchPart.Size = Vector3.new(branchLength, 0.1, 0.1)
branchPart.CFrame = branchPosition
table.insert(branches, branchPart)
end
for creatorId, branch in pairs(branches) do
local points = {}
local usedUpLength = 0
local branchLength = branch.Size.X
local lastPoint = CFrame.new(origin.Position, branch.Orientation) --CFrame.new(origin.Position, branch.Position)
local newBranches = 1
local currentDirection = 4
local branchLastPoint = branch.CFrame:ToWorldSpace(CFrame.Angles(0, currentDirection, 0)):ToWorldSpace(CFrame.new(branch.Size.X / 2, 0, 0)) --branch.CFrame:ToWorldSpace(CFrame.new(branch.Size.X / 2, 0, 0))
local branchId = 0
local currentBranchParts = {}
while true do
branchId = branchId + 1
local branchPart = Instance.new("Part", workspace)
branchPart.Material = "SmoothPlastic"
branchPart.Color = Color3.new(0, 0, 0)
branchPart.Anchored = true
branchPart.Material = "Neon"
branchPart.Transparency = 0
branchPart.CanCollide = false
branchPart.Name = "subbranch"..tostring(creatorId)..tostring(branchId)
local attemptId = 0
while true do
local length = math.random(1, 5) / 100
branchPart.Size = Vector3.new(length, 0.01, 0.01)
local direction = math.random(0, 1) + math.random(1, 9) / 10 + math.random(1, 9) / 100 + math.random(1, 9) / 1000 + math.random(1, 9) / 10000
if branchId == 1 then direction = 0 end
local origCurrentDirection = currentDirection
if currentDirection < 0 then
currentDirection = currentDirection + direction
elseif currentDirection > 0 then
currentDirection = currentDirection - direction
direction = direction * -1
end
branchPart.CFrame = lastPoint:ToWorldSpace(CFrame.Angles(0, 0, direction)):ToWorldSpace(CFrame.new(length / 2, 0, 0))
--branchPart.CFrame = lastPoint:ToWorldSpace(CFrame.Angles(0, direction, 0)):ToWorldSpace(CFrame.new(length / 2, 0, 0))
--if branchId == 1 then
-- branchPart.CFrame = lastPoint:ToWorldSpace(CFrame.new(length / 2, 0, 0))
--else
-- branchPart.CFrame = lastPoint:ToWorldSpace(CFrame.Angles(0, direction, 0)):ToWorldSpace(CFrame.new(length / 2, 0, 0))
--end
rayParams.FilterType = Enum.RaycastFilterType.Include
rayParams.FilterDescendantsInstances = whitelistParts
local rayResult = nil
if branchId > 2 then
rayResult = workspace:Raycast(lastPoint:ToWorldSpace(CFrame.new(0.1, 0, 0)).Position, CFrame.new(lastPoint.Position, branchPart.CFrame:ToWorldSpace(CFrame.new(branchPart.Size.X / 2 + 0.1, 0, 0)).Position).LookVector, rayParams)
end
for index, toWhitelistPart in pairs(toWhitelist) do
table.insert(whitelistParts, toWhitelistPart)
table.remove(toWhitelist, index)
end
if not rayResult then
table.insert(toWhitelist, branchPart)
table.insert(branchParts, branchPart)
table.insert(currentBranchParts, branchPart)
--local motor6D = Instance.new("Weld", enemyHumanoidRootPart)
--motor6D.Part0 = branchPart
--motor6D.Part1 = crackHandle
--branchPart.Anchored = false
lastPoint = branchPart.CFrame:ToWorldSpace(CFrame.new(branchPart.Size.X / 2, 0, 0))
newBranches = newBranches + 1
break
else
currentDirection = origCurrentDirection
attemptId = attemptId + 1
if attemptId >= 10 then
for _, pastBranchPart in pairs(currentBranchParts) do
pastBranchPart:Destroy()
branchId = branchId - 1
end
lastPoint = origin
attemptId = 0
end
branchPart:Destroy()
end
wait()
end
local relativeTo = branchLastPoint:ToObjectSpace(lastPoint)
if branchId == 40 then --relativeTo.Position.X >= 0 or branchId == 40 then --or branchId == 40 / 100 * enemyHumanoid.Health then
break
end
wait()
end
--if creatorId == 1 then break end
end
Here is what happens:
I want the branch circled in red to also go similarly to the others.
I’m aware of the fact I could have used better methods to find direction. No errors in the output.