- Home /
Pursuit Car AI that follows target on the track
Hello. I have an issue with a pursuit AI system. So I have a police car (AI controlled) that should follow the player while the player is racing in a circuit-like track. I am using the standard asset Car AI for the police, with the target being the transform of the player car. The circuit is built as a terrain with a drivable course on it.
I described my issue with an image. So the blue dots represent the police units, and the green and orange dots racer cars in 2 possible scenarios when it comes to position relative to the pursuing cop car. For the orange racer, the standard asset Car AI does a good job since the cop follows the orange car in a straight line, however for the 2nd scenario (green racer), if the cop car is behind a turn, it will try to go straight to the racer and crash/get out of the track.
Can someone tell me some way to modify the standard Asset car AI (preferably) to allow it to stay on track while pursuing a racer? From what i've seen, pathfinding should be a thing, but i couldn't find any good asset for it that can handle vehicle steering nor any tutorials about achieving this, and implementing my own pathfinding system that can handle this car ai controller seems quite huge.
Answer by Honorsoft · Mar 17, 2018 at 10:58 PM
There is at least two ways to accomplish this, either use a waypoint system (where there are waypoints in the middle of the road and placed every so often along your whole track/path) and your AI car will always attempt to follow those. Unfortunately, Unity's AI waypoint system needs to be gutted and rewritten due to flaws, so it will likely still be hard to get the Standard Assets AI Car Controller to stay on the road. But there is already a built-in waypoint system/method already in StandardAssets.
The second way is to use a 'target-follow' script to follow the target car, and a raycast coming from it to detect whether there is an obstacle (track border or debris on racetrack, etc.) in the way. If the path to the target car is not clear, it can keep adding steering or horizontal movement until it has cleared the obstacle, then continue with the plain target following. In your case, the car would steer LEFT to avoid the 'track wall'. You could also look into the NavMesh stuff in Unity, since it contains pathfinding that avoids obstacles already, you just have to set the track area as the only 'allowed' area for AI to travel on. There workarounds too, like a 2nd target position for the AI to follow that helps adjust a proper course.
In a racetrack scenario, you don't really need a direct 'follow the target' setup, because in a race all you have to consider is whether the AI has caught up to the player or not. The AI shouldn't cut across the track to catch the player, he always follows the track. So you could just have the AI drive until he is really close to the player, and that's when the code for 'interaction' happens, so that the AI won't try to cut corners chasing them. Basically, the AI ignores the player until really close. Also, you could avoid using waypoints all-together, and just have a script that adjusts the steering of the AI car to keep in the middle of the road/track (constantly checking the 'walls'). I hope some of this helps, I just started looking into using the StandardAssets car-controller instead of using my own, so I will probably run into the same problems. I already noticed issues with needless swerving to follow waypoints.
Your answer
Follow this Question
Related Questions
Cannot make initial speed for the Player car using AIDriverToolkit 0 Answers
How add kilometre per hour system in car script,how to add kilometre per hour speed system in script 0 Answers
drift car using rigidbody 1 Answer
how to rotate car to camera direction using wheel colliders. 1 Answer
A game like Need For Speed? 1 Answer