Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Automatic car keeps circling around waypoint, how can I fix it?

Asked by 1 year ago

I'm making a automatic car that uses pathfinding to find a way to drive to a destination but when it tries to it just does cicles around the waypoint but it does continue to the next one

What can I do to fix it and what is the problem with my code?

for _, waypoint in pairs(waypoints) do
        repeat

            local cframe = script.Parent.CFrame:ToObjectSpace(CFrame.new(waypoint.Position.X,script.Parent.Position.Y,waypoint.Position.Z))
            if (script.Parent.Position - Vector3.new(waypoint.Position.X,script.Parent.Position.Y,waypoint.Position.Z)).Magnitude < 20 then
                if cframe.X > 1 then
                    goright(turnspeed)
                elseif cframe.X < -1 then
                    goleft(turnspeed)
                elseif math.abs(cframe.X) < 0.5 then
                    goforwards(true)    
                else
                    goforwards(false)   
                    if cframe.X > 0.5 then
                        goright(turnspeed)
                    elseif cframe.X < 0.5 then
                        goleft(turnspeed)
                    end

                end
            else    
                goforwards(false)
                if cframe.X > 0.5 then
                    goright(5)
                elseif cframe.X < 0.5 then
                    goleft(5)
                end
            end
            game:GetService("RunService").Stepped:Wait()
        until (script.Parent.Position - Vector3.new(waypoint.Position.X,script.Parent.Position.Y,waypoint.Position.Z)).Magnitude < 5 or stopval == true 
        index += 1
        stop()
    end

heres the full script

local maxspeed = 20
local normalspeed = 10
local turnspeed = 0.2


function gobackwards()
    local FL = script.Parent.Parent.Front.FrontLeft
    local FR = script.Parent.Parent.Front.FrontRight
    local BL = script.Parent.Parent.Back.BackLeft
    local BR = script.Parent.Parent.Back.BackRight

    FL.AngularVelocity = -10
    FR.AngularVelocity = 10
    BL.AngularVelocity = -10
    BR.AngularVelocity = 10
end

function goforwards(speedornot)
    local FL = script.Parent.Parent.Front.FrontLeft
    local FR = script.Parent.Parent.Front.FrontRight
    local BL = script.Parent.Parent.Back.BackLeft
    local BR = script.Parent.Parent.Back.BackRight

    if speedornot == true then
        FL.AngularVelocity = maxspeed
        FR.AngularVelocity = -maxspeed
        BL.AngularVelocity = maxspeed
        BR.AngularVelocity = -maxspeed
    else
        FL.AngularVelocity = normalspeed
        FR.AngularVelocity = -normalspeed
        BL.AngularVelocity = normalspeed
        BR.AngularVelocity = -normalspeed
    end
end

function goleft(speed)
    local FL = script.Parent.Parent.Front.FrontLeft
    local FR = script.Parent.Parent.Front.FrontRight
    local BL = script.Parent.Parent.Back.BackLeft
    local BR = script.Parent.Parent.Back.BackRight

    FL.AngularVelocity -= speed
    FR.AngularVelocity -= speed
    BL.AngularVelocity -= speed
    BR.AngularVelocity -= speed
end

function goright(speed)
    local FL = script.Parent.Parent.Front.FrontLeft
    local FR = script.Parent.Parent.Front.FrontRight
    local BL = script.Parent.Parent.Back.BackLeft
    local BR = script.Parent.Parent.Back.BackRight

    FL.AngularVelocity += speed
    FR.AngularVelocity += speed
    BL.AngularVelocity += speed
    BR.AngularVelocity += speed
end

function stop()
    local FL = script.Parent.Parent.Front.FrontLeft
    local FR = script.Parent.Parent.Front.FrontRight
    local BL = script.Parent.Parent.Back.BackLeft
    local BR = script.Parent.Parent.Back.BackRight

    FL.AngularVelocity = 0
    FR.AngularVelocity = 0
    BL.AngularVelocity = 0
    BR.AngularVelocity = 0
end

local PathfindingService = game:GetService("PathfindingService")

local destination = game.Workspace.pathfindgoal

local function getAverageVelocity()
    local motorFL = script.Parent.Parent.Front.FrontLeft
    local motorFR = script.Parent.Parent.Front.FrontRight
    local motorBL = script.Parent.Parent.Back.BackLeft
    local motorBR = script.Parent.Parent.Back.BackRight

    local vFR = -motorFR.Attachment1.WorldAxis:Dot(motorFR.Attachment1.Parent.RotVelocity)
    local vRR = -motorBR.Attachment1.WorldAxis:Dot(motorBR.Attachment1.Parent.RotVelocity)
    local vFL = motorFL.Attachment1.WorldAxis:Dot(motorFL.Attachment1.Parent.RotVelocity)
    local vRL = motorBL.Attachment1.WorldAxis:Dot(motorBL.Attachment1.Parent.RotVelocity)
    return 0.25 * ( vFR + vFL + vRR + vRL )
end



function goto(dest)
    local stopval = false
    script.Parent.Anchored = false
    --script.Parent.AlignOrientation.MaxTorque = 10000000000
    local path = PathfindingService:CreatePath({
        AgentRadius = 9,
        AgentHeight = 9,
        AgentCanJump = false,
        AgentCanClimb = false,
    })

    path:ComputeAsync(script.Parent.Position, dest.Position)

    local waypoints = path:GetWaypoints()

    local index = 1

    for _, waypoint in pairs(waypoints) do
        local part = Instance.new("Part")
        part.Shape = "Ball"
        part.Material = "Neon"
        part.Size = Vector3.new(0.6, 0.6, 0.6)
        part.Position = waypoint.Position
        part.Anchored = true
        part.CanCollide = false
        part.Parent = workspace.bleuh
    end

    for _, waypoint in pairs(waypoints) do
        repeat

            local cframe = script.Parent.CFrame:ToObjectSpace(CFrame.new(waypoint.Position.X,script.Parent.Position.Y,waypoint.Position.Z))
            if (script.Parent.Position - Vector3.new(waypoint.Position.X,script.Parent.Position.Y,waypoint.Position.Z)).Magnitude < 20 then
                if cframe.X > 1 then
                    goright(turnspeed)
                elseif cframe.X < -1 then
                    goleft(turnspeed)
                elseif math.abs(cframe.X) < 0.5 then
                    goforwards(true)    
                else
                    goforwards(false)   
                    if cframe.X > 0.5 then
                        goright(turnspeed)
                    elseif cframe.X < 0.5 then
                        goleft(turnspeed)
                    end

                end
            else    
                goforwards(false)
                if cframe.X > 0.5 then
                    goright(5)
                elseif cframe.X < 0.5 then
                    goleft(5)
                end
            end
            game:GetService("RunService").Stepped:Wait()
        until (script.Parent.Position - Vector3.new(waypoint.Position.X,script.Parent.Position.Y,waypoint.Position.Z)).Magnitude < 5 or stopval == true 
        index += 1
        stop()
    end
    script.Parent.beep:Play()
    for _,v in pairs(workspace.bleuh:GetDescendants()) do
        v:Destroy()
    end
    --script.Parent.AlignOrientation.MaxTorque = 0
    spawn(function()
        wait(0.9)
        script.Parent.Anchored = true
    end)
end

while true do
    goto(workspace.start)
    wait(3)
    goto(destination)
    wait(3)
    goto(workspace.SpawnLocation)
    wait(3)
end

1 answer

Log in to vote
0
Answered by 1 year ago

turns out the turn speed was too high! didnt think that would be the problem but it was i lowered it to 0.1 and its fine now

0
put [SOLVED] or (answered) in your title T3_MasterGamer 2189 — 1y
Ad

Answer this question