- Home /
Steer towards target
So, this is somewhat of a loaded question and I will try to do my best to explain it.
I am creating a vehicle AI system, and the vehicle has a Steer(x) function. X ranges from -1(steer left) to 1(steer right).
I'm trying to find the value that I need to pass to that function to steer my vehicle accordingly to a target, in this case the next checkpoint.
An example: Imagine a vehicle to the right of a checkpoint, this function should return something like -0.2 to steer the vehicle back towards the checkpoint.
I've posted this question on other forums, and had some pretty indirect answers that more or less just confused me. All of them did bring up trig functions however, but it's somewhat difficult for me to understand being a freshmen in HS.
If somebody could provide any insight on the way to achieve something like this, it would be awesome. Thanks!
I have the exact same problem. I followed the unity wheel collider tutorial, but changed the steering part. So far my car just goes in circles. $$anonymous$$y code looks like this:
directionAngle += Input.GetAxis("Horizontal") * 0.01f;
rotationAngle = transform.rotation.y;
if (rotationAngle > $$anonymous$$athf.Sin(directionAngle))
{
steering = rotationAngle - $$anonymous$$athf.Sin(directionAngle);
}
else
{
steering = rotationAngle + $$anonymous$$athf.Sin(directionAngle);
}
float clampedSteering = $$anonymous$$athf.Clamp(steering, steeringDampening, -steeringDampening);
Answer by Kishotta · May 05, 2017 at 01:10 AM
I actually accomplished something similar by doing some vector manipulations. if a
is the vector from the vehicle to the target and b
is the target's forward direction, you can get a -1 to 1 steering value by doing Vector3.Dot (Vector3.Cross (a.normalized, b.normalized), Vector3.Up);
Vector3.Cross (a.normalized, b.normalized)
gets you a vector pointing either directly up or directly down (assuming you're on flat terrain) with lengths from 0 to 1. The dot product with Vector3.Up
will then give you a range from -1 to 1 with -1 being "Target is 90 degrees to the left" and 1 being "Target is 90 degrees to the right". Smaller steering values are given if you're more-or-less on the right track.
I don't have the code with me anymore, so I cant remember for sure if we used Vector3.Up
or Vector3.Down
, but it should be pretty easy to figure out which to use with trial and error.
Disclaimer, this ONLY works if the target is actually in front of the vehicle. If it's behind (even by just a little bit) you'll get the wrong steering. We solved this by having lots of checkpoints around curves, and generally not having and sharp (>90 degree) angles between checkpoints.
Thanks for the help,
I found that the AI system was really, well stupid. It seemed to try to over correct itself to much, where it positioned itself in the exact center of the next checkpoint. It would under correct, then over correct throwing it into a spiral.
Do you have any advice on how to make something like this "smarter"?
Oh wow. I was really not thinking when I answered this. It's only giving you -1 OR 1, not a range. I'm really sorry I didn't test that before answering.
You can get the magnitude of how much you should steer with Vector3.Cross (a.normalized, b.normalized)
and which way you should steer with Vector3.Dot (Vector3.Cross (a.normalized, b.normalized), Vector3.up)
.
Sorry for the confusion. Should look something like this:
Vector3 steerCross = Vector3.Cross (vehicleToWaypoint.normalized, transform.forward); // How much to steer (stored in the magnitude)
float steerDirection = Vector3.Dot (steerCross.normalized, Vector3.up); // Which way to steer (-1 for right +1 for left)
Steer = steerCross.magnitude * steerDirection; // $$anonymous$$ultiply them together to get get final steering
"Disclaimer, this ONLY works if the target is actually in front of the vehicle. If it's behind (even by just a little bit) you'll get the wrong steering. We solved this by having lots of checkpoints around curves, and generally not having and sharp (>90 degree) angles between checkpoints."
There has to be a way to do this without this happening?
Your answer
Follow this Question
Related Questions
Buildable objects 0 Answers
vehicle tools 0 Answers
Camera rig warning! Auto Cam Script 1 Answer
Pathfinding on Instantiated AI Vehicles 0 Answers
Buildable parts 0 Answers