Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Start = script.Parent.Start
- End = script.Parent.End
- function Try(position, position2)
- --ray = Ray.new(position, (End.Position - position).unit * (End.Position - position).magnitude)
- part, place = game.Workspace:FindPartOnRay(Ray.new(position, (position2 - position)))
- if part == End then
- return true
- else
- return false
- end end
- function FireRays(position, rays)
- local part = Instance.new("Part", workspace.Ignore)
- part.BrickColor = BrickColor.new("Bright green")
- part.Anchored = true
- part.Size = Vector3.new(1, 2, 1)
- part.CFrame = CFrame.new(position)
- --game.Debris:AddItem(part, 30)
- local places = {}
- for i = 1, rays do
- local part, place = game.Workspace:FindPartOnRay(Ray.new(position, ((CFrame.new(position) * CFrame.Angles(0, math.rad(360/rays * i), 0) * CFrame.new(10, 0, 0)).p - position) .unit * 300), game.Workspace.Ignore)
- place = (CFrame.new(place) * (CFrame.Angles(0, math.rad(360/rays * i), 0) * CFrame.new(-0.1, 0, 0))).p
- if place and part then
- local part = Instance.new("Part", workspace.Ignore)
- part.BrickColor = BrickColor.new("Bright red")
- part.Anchored = true
- part.Size = Vector3.new(1, 1, 1)
- part.CFrame = CFrame.new(place)
- --game.Debris:AddItem(part, 30)
- end
- if part then
- table.insert(places, place)
- end end
- return places
- end
- function findintable(tab, object)
- local g = false
- for i = 1, #tab do
- if tab[i] == object then
- g = i
- break
- end end
- return g
- end
- function Scan(destination, position, info, ...)
- if not findintable(info.visited, position) and not info.foundtarget then
- local closest = 5000
- for i = 1, #info.visited do
- if closest > (info.visited[i] - position).magnitude then
- closest = (info.visited[i] - position).magnitude
- end end
- if closest > 2 then
- table.insert(info.visited, position)
- local hits = FireRays(position, 5)
- local inrange = {}
- for i = 1, #hits do
- if Try(hits[i], destination) then
- table.insert(inrange, hits[i])
- end end
- if #inrange == 0 then
- for i = 1, #hits do
- args = ...
- delay(0, function ()
- Scan(destination, hits[i], info, hits[i], args)
- end)
- end
- else
- local closest = inrange[1]
- for i = 2, #inrange do
- if (inrange[i] - destination).magnitude < (closest - destination).magnitude then
- closest = inrange[i]
- end end
- info.foundtarget = {..., closest}
- end end end end
- function Pathfind(p1, p2)
- local vals = {}
- vals.visited = {}
- vals.foundtarget = false
- Scan(p2.Position, p1.Position, vals)
- repeat wait() until vals.foundtarget
- return foundtarget
- end
- --[[starttime = tick()
- Pathfind(Start, End)
- endtime = tick()
- End.BrickColor = BrickColor.new("Really red")
- print("Target found. Time elapsed: ".. endtime - starttime)]]
- debounce = false
- game.Workspace.SL.CD.MouseClick:connect(function ()
- if not debounce then
- End.BrickColor = BrickColor.new("Bright green")
- game.Workspace.Ignore:Destroy()
- Instance.new("Model", workspace).Name = "Ignore"
- debounce = true
- Pathfind(Start, End)
- End.BrickColor = BrickColor.new("Really red")
- debounce = false
- end end)
Advertisement
Add Comment
Please, Sign In to add comment