You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? I am working on an AI car that can avoid obstacles (it currently only chases the player ignoring any obstacles).
What is the issue? My goal is for the car to steer away from walls and other obstacles it encounters. However, I’m having trouble figuring out how to achieve this.
What solutions have you tried so far? I have tried integrating code from the Roblox pathfinding documentation* (also tried adding raycasting to my code) and searching for tutorials on YouTube and other platforms, but most of the information I have found is about humanoid pathfinding. I have also read every article on the DevForum about this topic.
*Character Pathfinding | Roblox Creator Documentation
*PathfindingService | Roblox Creator Documentation
I am here as a last resort, would really appreciate any tips or advice!
This is my current code:
wait(2)
Constraints = script.Parent.Parent.Constraints
Right = Constraints.Right
Left = Constraints.Left
RR = Constraints.RR
RL = Constraints.RL
FR = Constraints.FR
FL = Constraints.FL
FrontVector = script.Parent.Parent.Body.FrontVector
RR.AngularVelocity = -50
RL.AngularVelocity = -50
FR.AngularVelocity = -50
FL.AngularVelocity = -50
game:GetService(“RunService”).Heartbeat:Connect(function()
local NearestPlayer = nil
local NearestDistance = math.huge
for _, player in pairs(game.Players:GetPlayers()) do
local humanoid = player.Character and player.Character:FindFirstChildOfClass(“Humanoid”)
if humanoid and humanoid.Health > 0 then
local distance = (player.Character.HumanoidRootPart.Position - FrontVector.Position).Magnitude
if distance < NearestDistance then
NearestPlayer = player
NearestDistance = distance
end
end
end
if NearestPlayer then
local PositionDifference = (NearestPlayer.Character.HumanoidRootPart.Position - FrontVector.Position).Unit
local GoalFrontVector = FrontVector.CFrame.LookVector
local DotProduct = PositionDifference:Dot(GoalFrontVector)
local Angle = DotProduct
local TargetAngle1 = math.deg(math.acos(Angle))
local TargetAngle = TargetAngle1 - 90
if TargetAngle < 90 then
TargetAngle = TargetAngle
end
if TargetAngle > 90 then
TargetAngle = 90 - TargetAngle
end
Right.TargetAngle = TargetAngle
Left.TargetAngle = TargetAngle
print(TargetAngle)
end
local NearestPlayer = nil
local NearestDistance = math.huge
for _, player in pairs(game.Players:GetPlayers()) do
local humanoid = player.Character and player.Character:FindFirstChildOfClass(“Humanoid”)
if humanoid and humanoid.Health > 0 then
local distance = (player.Character.HumanoidRootPart.Position - FrontVector.Position).Magnitude – Calculate distance from car
if distance < NearestDistance then
NearestPlayer = player
NearestDistance = distance
end
end
end
if NearestPlayer then
local PositionDifference = (NearestPlayer.Character.HumanoidRootPart.Position - FrontVector.Position).Unit
local GoalFrontVector = FrontVector.CFrame.LookVector
local DotProduct = PositionDifference:Dot(GoalFrontVector)
local Angle = DotProduct
local TargetAngle1 = math.deg(math.acos(Angle))
local TargetAngle = TargetAngle1 - 90
if TargetAngle < 90 then
TargetAngle = TargetAngle
end
if TargetAngle > 90 then
TargetAngle = 90 - TargetAngle
end
Right.TargetAngle = TargetAngle
Left.TargetAngle = TargetAngle
print(TargetAngle)
end
end)