#dogwater "ai" turning

1 messages · Page 1 of 1 (latest)

tacit bronze
#

i cant fit my script here but heres the function


local function auto()
    local closest
    local dist = math.huge

    for _, p in workspace:getdescendants() do
        if p:isa("basepart") and p.name:lower() == "turnend" then
            local d = (p.position - bus:getpivot().position).magnitude
            if d < dist then
                dist = d
                closest = p
            end
        end
    end

    if not closest then return end

    local v = closest.cframe.lookvector
    local a = math.deg(math.atan2(v.x, v.z))
    a = (a + 180) % 360 - 180

    local cur = yaw(bus:getpivot())
    local gap = yawgap(a, cur)

    turnby(gap)
end

#

if i can script that good, i would've made it path find

slender surge
#

it would be beneficial to use pathfindingservice as its less clutterfull and easier in general

tacit bronze
#

i use parts

#

engine.touched:connect(function(hit)
if not hit:isa("basepart") then return end
local name = hit.name:lower()

local partline = hit:findfirstchildwhichisa("numbervalue")
if partline and line and partline.value ~= line.value then return end

if name == "stop" and not locked then
    locked = true
    moving = false
    doors(true)

    local txt, dest = "", ""
    local l = hit:findfirstchild("setline")
    if l and l:isa("numbervalue") then txt = tostring(l.value) end

    local d = hit:findfirstchild("setdest")
    if d and d:isa("stringvalue") then dest = d.value end

    signs(txt, dest)

    task.wait(14)
    doors(false)
    task.wait(6)
    moving = true
    locked = false

elseif name == "left" then
    turnby(90)

elseif name == "right" then
    turnby(-90)

elseif name == "turn" then
    auto()

elseif name == "lane left" then
    task.spawn(function()
        spin.angularvelocity = vector3.new(0, 0.5, 0)
        task.wait(0.3)
        spin.angularvelocity = vector3.zero
        task.wait(2)
        spin.angularvelocity = vector3.new(0, -0.5, 0)
        task.wait(0.3)
        spin.angularvelocity = vector3.zero
    end)

elseif name == "lane right" then
    task.spawn(function()
        spin.angularvelocity = vector3.new(0, -0.5, 0)
        task.wait(0.3)
        spin.angularvelocity = vector3.zero
        task.wait(2)
        spin.angularvelocity = vector3.new(0, 0.5, 0)
        task.wait(0.3)
        spin.angularvelocity = vector3.zero
    end)

elseif name == "speed" then
    local s = hit:findfirstchild("speed")
    if s and s:isa("numbervalue") then
        maxspeed = s.value
    end
end

end)

slender surge
#
local pathfindingservice = game:GetService("PathfindingService")

local path = pathfindingservice:CreatePath() -- create a path
path:ComputeAsync() -- compute the path with start and end positions

local waypoints = path:GetWaypoints() -- get the waypoints

for i,v in pairs(waypoints) do
    -- move the humanoid to each individual waypoint
end

here is a small, commented version of basic pathfinding you can build off of

tacit bronze
#

for turns and obstacles?

slender surge
#

jumping can be added seperately

tacit bronze
#

okay

#

buses only move

#

but how am i going to add that with like body velocity

tacit bronze
#

cause i dont like it stuttering

slender surge
#

you can use humanoid:moveto()

slender surge
#

well pathfindingservice gives over waypoints

#

which is basically an array of parts

#

with that in mind you can set targets to those waypoints

tacit bronze
#

i use this:
`task.spawn(function()
while true do
local dir = bus:getpivot().lookvector
local diff = target - speed
local step = diff > 0 and accel or brake

    if math.abs(diff) > 0.1 then
        speed += math.sign(diff) * step * 0.1
    else
        speed = target
    end

    if velo then
        velo.velocity = dir * speed
    end

    task.wait(0.1)
end

end)`

#

which moves it in the look vector

slender surge
tacit bronze
#

combining it with the pathfinding

slender surge
#

as waypoints are just parts

#

part.CFrame.Lookvector

tacit bronze
#

and the stuttering,

#

its pretty noticiable

slender surge
#

can you send over an instance of stuttering

tacit bronze
#

but how am i going to combine

random escarpBOT
#

studio** You are now Level 1! **studio

tacit bronze
#

combining my script with the path finding

#

plus using body velocity

slender surge
# tacit bronze but how am i going to combine

in your function first compute a path

local pathfindingservice = game:GetService("PathfindingService")

local path = pathfindingservice:CreatePath() -- create a path
path:ComputeAsync() -- compute the path with start and end positions

then acquire the waypoints

local waypoints = path:GetWaypoints() -- get the waypoints

then move using lv

task.spawn(function()
    while true do

        for i,v in pairs(waypoints) do
                    local dir = v.CFrame.Lookvector
                    local diff = target - speed
                    local step = diff > 0 and accel or brake
            
                    if math.abs(diff) > 0.1 then
                        speed += math.sign(diff) * step * 0.1
                    else
                        speed = target
                    end
            
                    if velo then
                        velo.velocity = dir * speed
                    end
        end

        task.wait(0.1)
    end
end)
#

while computing you need a start position and a end position!

tacit bronze
#

and then it will compute a path

#

oh okay

#

thanks

slender surge