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:
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
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.
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!
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.
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?
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