How do I make an attachment always rotate toward the nearest waypoint?

Hello, I am trying to make a vehicle AI for my personal chassis. I want to make this vehicle’s attachments rotate toward the nearest waypoints. The attachments Y axis in the orientation make the wheels turn. The thing is, how do I script them to always be rotated to the nearest AI waypoint.
Example: I have 5 waypoints. They are called AIWaypoint(number), and they are in a model. I want the vehicle to rotate the FL and FR attachments to the nearest specific AI waypoint in that model, so it can follow paths and make turns. But I have no idea how to script this. I asked here to get help. Please no jokes.
The vehicle has a model named Chassis, and there is a part named Platform. There are attachments named AttachmentFL and AttachmentFR. If you rotate them both on the Y axis, they turn like a real car.
Here is the link to the model of the car, for further investigation: a - Roblox

Note: The car needs collision groups to work properly, they are set up like this:

1 Like

There must be some math formula that determines the correct rotation. However, I suggest trying to use BodyGyro and set CFrame to the waypoint(number) CFrame. And also set the MaxTorque to something like (0, 10000, 0) so it only rotates on the Y axis.

This should be the easiest way to do it. However, having both wheels rotate to the waypoint might not be the best way to solve your problem. Imagine what happens when you get very close to a waypoint and then both wheels turn inwards (pointing to one another). I can only imagine that it will not be good.

I do not know if you have constraints on the wheels. That might actually solve the pointing inward problem :smiley:

How it works, is that the vehicle’s turning is scripted. It is pretty simple. I have the SteerFloat mapped to the attachment orientation. Yes I am not using that Ackermann science thing, but not too smart to do that haha

The BodyGyro will modify the wheels orientation. Make sure to have one in each wheel and set both target CFrames at the same time. Update me if you manage to make it work.

Sounds like a cool problem.

Hmm, how would I CFrame that BodyGyro? What properites would that BodyGyro have? Each wheel? I just want the FL and FR to turn.
Sorry I am not too fond of CFraming.
Also I used Crazyman32’s tutorial on how to rig a car.

You must put one body gyro in each part that you want to turn (I assumed wheels).
When you want to set up a new waypoint you will have to do

bGyro.CFrame = Waypoint1.CFrame

Simple, and you must do it for both wheels. Other than that, leave the default body gyro settings and change the MaxTorque as I explained in the previous post.

Uh oh, that doesn’t want to work. No error in output. Not turning. Like I said there is a link in the main post to look at the car. Remember to set up coll groups!

Update: I tried doing it by using

bGyro.CFrame = CFrame.new(Waypoint1.Position, script.Parent.Parent.Position)

but for some reason the vehicle just rotating in some odd way. How do I fix this? The wheels aren’t rotating, but for some reason the car is just rotating weirdly.

you have it inverted. The first half of the cframe is the position, and the second half is the LookAt.
what you should do is

bGyro.CFrame = CFrame.new(script.Parent.Parent.Position, Waypoint1.Position)

Good, now it is working? Now onto the second half. How do I define Waypoint1 to be the nearest part that the vehicle body finds in a model?

use this function

local function findClosestWaypoint()
     local currentsmallestmag, object = math.huge, nil;
     for _, v in pairs(workspace.WayPoints:GetChildren()) do -- if you are using a folder to organize your waypoints
          local thismag = (v.Position - script.Parent.Parent.Position).Magnitude
          if thismag  < currentsmallestmag then
                 currentsmallestmag = thismag
                 object = v
          end
     end
     return object
end

local closestWaypoint = findClosestWaypoint() -- prints closest waypoint
bGyro.CFrame = CFrame.new(script.Parent.Parent.Position, closestWaypoint.Position)

I made the vehicle move by using script.Parent.Velocity = script.Parent.CFrame.lookVector *100, but for some reason when the vehicle gets to the waypoint, it flies. How do i stop it from flying like that?

hmm can you show your code? it sounds like a collision problem.

I have the waypoints anchored and non-CanCollide.

while wait() do
script.Parent.Velocity = script.Parent.CFrame.lookVector *100
end

that is weird, but on a side note, you should probably point it in the direction of the waypoint instead of only using the waypoint. Like this

script.Parent.Velocity = CFrame.new(script.Parent.Parent.Position, closestWaypoint.Position).LookVector * 100

or like this

script.Parent.Velocity = (closestWaypoint.Position - script.Parent.Parent.Position).Unit * 100

Hmm, ClosestWaypoint is not defined. btw I have the BodyGyro code in the wheels and the moving script in the body

you should just use the one script in the body, but combine all the code. I recommend you use a BodyPosition and a BodyGyro for this instead of setting the velocity

I combined the code and put the BodyGyro in the body, but for some reason the vehicle is getting slammed to the ground.

1 Like

look at these articles

script.Parent.Velocity = CFrame.new(script.Parent.Parent.Position, closestWaypoint.Position).LookVector * 100

It appears to be that which is causing the vehicle to be slammed to the ground. But how do I fix that?