Deciding on which waypoint to go

i have this module right here which handles the waypoint stuff

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

local CarController = require(ReplicatedStorage.Common.CarController)

local AI = {}
local Static = {}

--[[
    0 - Stop
    1 - Proceed
]]
local Signals = {
    ['Institutional white'] = 1;
    ['Really blue'] = 1;

    ['Really black'] = 0;
}

function AI:init(car: Model)
    return setmetatable({
        car = car;
        controller = CarController.new(car);
        range = 10
    }, {__index = Static})
end

function Static:cycle(waypoints: {BasePart}, start: number)
    -- Increment safely
    local function increment(n)
        return (n+1) % (#waypoints:GetChildren())
    end

    -- The current index
    local index = start

    -- Get the current waypoint and the next waypoint
    local waypoint = waypoints:FindFirstChild(index)
    local target = waypoints:FindFirstChild(increment(index))

    -- Pivot the car to the current waypoint
    self.car:PivotTo(waypoint.CFrame)

    return RunService.Heartbeat:Connect(function()
        -- Get the signal
        local signal = Signals[waypoint.BrickColor.Name]

        -- Get the distance from target to the car
        local dist = (target.Position - self.car.Sensors.Front.Position).Magnitude

        -- If reached a certain distance, increment the index
        if dist < self.range then
			index = increment(index)
        end

        if signal == 0 then
            self.controller:Accelerate(0)
        elseif signal == 1 then
            self.controller:MoveTo(target.Position)
        end

        -- Update the waypoint and the target
        waypoint = waypoints:FindFirstChild(index)
        target = waypoints:FindFirstChild(increment(index))
    end)
end

return AI

and because of i was planning it to have intersections and traffics, i want it to decide on which waypoint to waypoint to go

how can i do that?

There’s really not much insight that can be offered.

  • How far have you got into testing this system with other AI traffic?
  • Do vehicles collide when they shouldn’t?
  • What have you tried so far?

Can you show pictures or a video of what is currently occurring, vs what you’re expecting to happen?


I have waypoints that are grouped as a “network” which are visualized by the purple box

image
I got the intersection thing figured out, they can diverge and merge to another network

I currently only have 1 vehicles for testing

I am currently trying to implement this system into my module

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.