Unsure if anyone has already posted this or not but I encountered some people asking for it so
I’ve made a function that automatically generates attachments (Or Vector3):
function raycastHitbox:DistributePoints(obj: BasePart | Bone, gridRes: IntValue | Vector3, coverVolume: BoolValue)
assert(gridRes <= 1, "Number of points on each side should be greater than 1")
local offsetTbl = {}
gridRes = typeof(gridRes) == "Vector3" and gridRes or Vector3.new(1, 1, 1) * gridRes
-- how many points should be generated on each axis
local PointsTbl = {
X = {},
Y = {},
Z = {}
}
for axis, axisTbl in pairs(PointsTbl) do
for i = 0, gridRes[axis] - 1 do
print("loop")
local point = ((i / (gridRes[axis] - 1)) * obj.Size[axis]) - obj.Size[axis] / 2
-- calculate the percentage then minus the middle point to find the offset
table.insert(axisTbl, point)
end
end
for _, x in pairs(PointsTbl.X) do -- recursion
for _, y in pairs(PointsTbl.Y) do
for _, z in pairs(PointsTbl.Z) do
if not coverVolume then
-- if set to true the points will fill up the whole part,
-- if not then it will only cover the surface (Default: Surface)
if x == PointsTbl.X[1] or x == PointsTbl.X[#PointsTbl.X]
or y == PointsTbl.Y[1] or y == PointsTbl.Y[#PointsTbl.Y]
or z == PointsTbl.Z[1] or z == PointsTbl.Z[#PointsTbl.Z] then
-- cover the surface only by limiting the value to the corners
table.insert(offsetTbl, Vector3.new(x, y, z))
end
continue
end
table.insert(offsetTbl, Vector3.new(x, y, z))
end
end
end
self:SetPoints(obj, offsetTbl)
end
I haven’t benchmarked this yet but I’m sure it is more costly than manually setting up points yourself, although if the gridRes
value is smaller than 7 then it shouldn’t be that impactful.
Nevertheless there are definitely rooms for improvement, Eg: removing the recursions and make the algorithm applicable to meshes / parts that aren’t triangle
(It will still work on those objects it just wouldn’t be as accurate since you will be covering up air essentially, unless you want pinpoint accuracy then personally I don’t find this a major problem)
In action: https://gyazo.com/a0af1b9b8b63eecc225080b413d1f698
Edit: I have tested the recursion, I putted in 10 gridRes
and it looped around 1500 times (Which might be worrying), so keep that in mind before you fully use this. If anyone else has an alternative that doesn’t involve recursion it would be profusely appreciated if you share it with us!.
Depending on how big your weapons are I think 3 or 5 is a reasonable number