Help making an Auto-Drive chassis car

So im trying to make a chassis car drive around but I want it to follow walkpoints so how would I do that?

local car = script.Parent.Parent
local ScriptsFolder = car:FindFirstChild("Chassis")
local Chassis = require(ScriptsFolder:WaitForChild("Chassis"))
Chassis.InitializeDrivingValues()
Chassis.Reset()

local currentVel = Chassis.GetAverageVelocity()
local straight = false
local straight1 = false
local steer = 0
local throttle = 0

throttle = 0.2

Chassis.UpdateSteering(steer, currentVel)
Chassis.UpdateThrottle(currentVel, throttle)

This is my car script to make it go forward but I want it to follow walkpoints like Point A to Point B

To make it follow waypoints you would need to use a lot of maths.

First, get the cars current position and goal waypoint. Next, the angle between the look vector and the goal to calculate the steer and then you can formulate an equation which would steer the car towards the goal. You would need to account for the speed of your car and turning angle. I can’t give you the formula because I don’t know it, I could formulate it but I currently can’t.

All the best.

Use PathfindingService and a mover constraint of your choice, like this:

local path = PathFindingService:CreatePath()

local function driveTo(startingPos, endingPos)

	path:ComputeAsync(startingPos, endingPos)

	carPrimaryPart:SetNetworkOwner(nil)

	local wayPoints = path:GetWaypoints()
	local waypoint = 2
	local moveToConnection

	local linearVelocity = Instance.new("LinearVelocity")
	linearVelocity.MaxForce = math.huge
	linearVelocity.RelativeTo = Enum.ActuatorRelativeTo.World
	linearVelocity.Enabled = true

	local attachment = Instance.new("Attachment")
	attachment.Parent = carPrimaryPart

	linearVelocity.Attachment0 = attachment
	linearVelocity.Parent = carPrimaryPart

	local direction 
	for i, waypoint in wayPoints do
		direction = (wayPoints[2].Position - carPrimaryPart.Position).Unit
		if (carPrimaryPart.Position - waypoint.Position).Magnitude < 1 then

		else
			linearVelocity.VectorVelocity = direction * carPrimaryPart.AssemblyMass * 0.06
		end

	end

	linearVelocity.VectorVelocity = direction * carPrimaryPart.AssemblyMass * 0.06

end

driveTo(carPrimaryPart.Position, Position) -- Position is the position you want it go to

Unless the waypoints has obstacles, you won’t need to compute a path for every waypoint. The OP specifically asked to use a chassis module, not a mover instance

1 Like

I have an old project of mine regarding autonomous cars, and boy do I have the solution for you

For the steering angle (in degrees):

local diff = (target - car.Position).Unit
local dot = diff:Dot(car.CFrame.LookVector)
local angle = math.deg(math.acos(dot)) - 90
angle = if angle > 90 then 90 - angle else angle

The throttle is up to you, but I made it slow down the closer it gets:

local dist = (target - car.Position).Magnitude
local speed = MAX_SPEED

if dist < DECELERATION_DIST then
    speed = MAX_SPEED * (dist / DECELERATION_DIST)
end

I’m sure they’re self-explanatory, just plug them to your chassis module functions (and define the variables)

1 Like

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