So what I am trying to code is a custom path generator algorithm.
What it does is it generates parts between 2 parts with the given amount of studs.
Here are some screenshots of my current progress:
local Studs = 5
local function RayCast()
local P1 = script.Parent.P1
local P2 = script.Parent.P2
local Target = script.Parent.PT
local Multiplier = 2
local Mag = (P1.Position - P2.Position).magnitude / (Studs*Multiplier)
local AmountOfPartsRequired = math.round(Mag)
print(AmountOfPartsRequired)
local ray = Ray.new(P1.Position, (P1.Position - P2.Position).unit * Mag)
local part, position = workspace:FindPartOnRay(ray)
local Parts = {}
--
repeat AmountOfPartsRequired = AmountOfPartsRequired-1
local Stud = Instance.new("Part")
Multiplier = Multiplier - Multiplier/2
print(Multiplier)
Mag = (P1.Position - P2.Position).magnitude / (Studs*Multiplier)
ray = Ray.new(P1.Position, (P1.Position - P2.Position).unit * Mag)
Stud.CFrame = CFrame.new(P1.Position, position) * CFrame.new(0,0,Mag)
table.insert(Parts, Stud)
until AmountOfPartsRequired<1
Target.CFrame = CFrame.new(P1.Position, position) * CFrame.new(0,0,Mag)
return Parts
end
game["Run Service"].Heartbeat:Connect(function()
local Ps = RayCast()
for i=1, #Ps do
Ps[i].Parent = game.Workspace.NodesHolder
end
wait()
Ps = game.Workspace.NodesHolder:GetChildren()
for i=1, #Ps do
Ps[i]:Destroy()
end
end)
Nvm I figured it out myself.
here is how I made it:
In this script, I define the start and end positions using Vector3 values, and the distance between each stud using the studDistance variable. We then calculate the direction and distance between the start and end positions using Vector3 operations.
Next, I made a loop that generates a series of Part objects representing the studs along the path. I incremed the current position along the direction vector by studDistance in each loop iteration, create a new Part object at that position, and set its CFrame to the new position. Finally, I added a final stud at the end position.
script.
-- Define the start and end positions
local function GeneratePath()
local start = script.Parent.P1.Position
local finish = script.Parent.P2.Position
-- Define the distance between each stud in the path
local studDistance = 5
-- Calculate the direction and distance between the start and end positions
local direction = (finish - start).Unit
local distance = (finish - start).Magnitude
-- Create the path using studs
local currentPos = start
while (currentPos - finish).Magnitude > studDistance do
currentPos = currentPos + direction * studDistance
local stud = Instance.new("Part")
stud.Name = "PathStud"
stud.Size = Vector3.new(1.2, 1.2, 1.2)
stud.CFrame = CFrame.new(currentPos)
stud.Anchored = true
stud.Parent = game.Workspace.NodesHolder
end
-- Add a final stud at the end position
end
game["Run Service"].Heartbeat:Connect(function()
Ps = game.Workspace.NodesHolder:GetChildren()
for i=1, #Ps do
Ps[i]:Destroy()
end
GeneratePath()
end)