- Home /
Most Efficient Method for Steering for Vehicle AI
Hi Unity Community!
Any tutorial regarding vehicle AI gives the following code for getting the steering magnitude (-1 to 1) to the next waypoint:
Vector3 pos = transform.InverseTransformPoint(nextWaypoint.position);
steerMagnitude = pos.x / pos.magnitude;
But since this involves the Sqrt operation, is there a more efficient method? Because I plan on having about minimum 50 vehicles in the scene at a time. But that can even increase to 100 - 200 vehicles. Or is the Sqrt operation fast enough for mobile devices to cope with 200 vehicles using this code each FixedUpdate? I also have other code like OA, etc.
That formula looks wrong. All it says is to crank the steering wheel linearly based on angle to the target. I assume this needs to run every frame, so you even out eventually. It might work (almost anything will) but it isn't how people drive.
For real, you try to aim at the target as quickly as possible -- if a target is only a little to the right, you still make a "standard turn," and only even out the wheel as you face it.
So I devised a simple but effective formula (since the current steer magnitude formula returns steering magnitude if the steerAngle was 90 degrees. So I just used the unitary method xD:
steer$$anonymous$$agnitude = pos.x / pos.magnitude * 90 / steerAngle;
Shameless bump, but looking at this question, I realised what kind of a noob I was almost 2 years ago!
Answer by tanoshimi · Dec 16, 2014 at 02:15 PM
What's your fixed timestep? I think the default is 0.02, but there's no way when I'm driving I adjust by steering 50 times a second.... if you need to keep the timestep that high for other calculations, then don't recalculate the steering magnitude in every FixedUpdate() - put it in a coroutine, say, that runs less frequently.
That's a good idea. I'll try that later. Is there any limit for the number of coroutines running simultaneously? Do a lot of coroutines running together have an impact on performance? Since I have a lot of coroutines running forever (but waiting for few seconds after two-three statements) in my other code also.
Coroutines don't run "together" - they still run on the main thread, but since they remember their internal state and resume from where they got to after each yield, they allow you to spread calculations over several frames.
Your answer
Follow this Question
Related Questions
Determining rotation for collision avoidance? 3 Answers
Turning an object towards point 0 Answers
makeing enemies 1 Answer
car movement and steering AI algorithms 2 Answers
unitysteer and path 1 Answer