I want achieve a AI Car system where it follows waypoints when its steering in the middle of the waypoint perfectly as possible.
The issue is, i don’t know how to code that.
Tried Vector3 but, it didn’t work.
while wait(0.001) do
wait(0.001)
local PathfindingService = game:GetService("PathfindingService")
local TweenService = game:GetService("TweenService")
-- Variables for the car and destination, etc.
local carprimary = script.Parent.Main
local destination = game.Workspace.Path
local path = PathfindingService:CreatePath()
path:ComputeAsync(carprimary.Position, destination.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
local part = Instance.new("Part")
part.Shape = "Ball"
part.Material = "Neon"
part.Name = "Pathfind_Point"
part.Size = Vector3.new(0.6, 0.6, 0.6)
part.Position = waypoint.Position
part.Anchored = true
part.CanCollide = false
part.Parent = game.Workspace
--carprimary:MoveTo(waypoint.Position)
--local tween = TweenService:Create(carprimary, TweenInfo.new(0.75), {Position = waypoint.Position})
--tween:Play()
wait(0.001)
end
wait(0.001)
end
you can see here that the car has a middle green part so it stops the steering when it hits.
But is there a way to make the AI to to see 180 degrees seperately so it steers to part and stops steering to it slowly?
Script lines of the fixed issues would be helpful.
You could use CFrame.lookAt to create a new CFrame that’s facing the point perfectly. You can then use Lerp to tween the CFrame of the car to the CFrame lookAt made, for a simple solution.
For a more complicated solution, you need to know a bit of math, but you can use CFrame.Angles to create a rotation, and just give it a Y value. That Y value can be gotten by using the current LookVector of the car, and the difference vector from the car’s current position, to the goal position (Goal.Position - Car.Position).Unit With those two vectors, if you use the Dot Product on them, you can get the amount of angle, and with Cross Product (checking if the resulting vector’s Y is positive or negative) you can get whether it should steer left or right. The actual angle you want to steer by should be a fraction of the angle that the dot product gives you. You can add the Rotation CFrame that CFrame.Angles gave you, to the car’s CFrame by doing Car:PivotTo(Car:GetPivot()*Rotation)
Sir thats not what i meant, How do i give my a car vision? but the 180 degrees are seperated into 2 for left and right i want it to be like If it detects the left side has the way point steer left to it , if it detects the right side has the way point steer right to it. Im sorry if i was making this misunderstandable.
If the dot product’s result is larger than 90* (which in radians, is math.pi/2) then you can just have it not steer. The cross product is what lets you know if it needs to steer left or right.
Hi sir, as you can see i still can not fix my problem, im only a beginner, could you make the code so it will steer at pathfinding and when its perfectly in the position of the pathfinding stop steering.
while wait(0.001) do
wait(0.001)
local PathfindingService = game:GetService("PathfindingService")
local TweenService = game:GetService("TweenService")
-- Variables for the car and destination, etc.
local carprimary = script.Parent.Main
local destination = game.Workspace.Path
local path = PathfindingService:CreatePath()
path:ComputeAsync(carprimary.Position, destination.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
local part = Instance.new("Part")
part.Shape = "Ball"
part.Material = "Neon"
part.Name = "Pathfind_Point"
part.Size = Vector3.new(0.6, 0.6, 0.6)
part.Position = waypoint.Position
part.Anchored = true
part.CanCollide = false
part.Parent = game.Workspace
--carprimary:MoveTo(waypoint.Position)
--local tween = TweenService:Create(carprimary, TweenInfo.new(0.75), {Position = waypoint.Position})
--tween:Play()
wait(0.001)
end
wait(0.001)
No, but I can answer any questions you have. I did spot a mistake in the logic of your code though, that may help. You have your car movement code inside your for loop, that’s looking at all the waypoints. You will instead just want your car to path towards only one waypoint at a time, running your movement code there will just make your car jitter back and forth, re-setting its position a bunch.