Creating intelligent chase/follow ai.

I am currently creating an open world racing game for mobile. As of right now, I am stuck on creating intelligent ai for the police cars and other pursuers. The issue is collision detection. While they know how to drive towards the target and can predict the target’s future position, the ai is incapable of detecting and avoiding collisions. Navmeshes have terrible performance on mobile (tested), so that is not a possible solution. I am currently attempting to use raycasts, but the police cars tend to behave unpredictably, as they sometimes don’t detect collisions at all, and other times swerve and make wrong turns that take them too far from the player. What is the best approach to collision detecting and avoiding for AI?

This is my current raycast logic:

RaycastHit Right;
            float m_right;
            if (Physics.Raycast (m_transform.TransformPoint (raycastPoint) + ((carWidth / 2) * m_transform.right), m_transform.forward, out Right, magnitude, CollisionDetectionLayers.value)) {
                m_right = (-1f) + Right.distance / magnitude;
            } else {
                m_right = 0;
            }


            RaycastHit Left;
            float m_left;
            if (Physics.Raycast (m_transform.TransformPoint (raycastPoint) + ((-carWidth / 2) * m_transform.right), m_transform.forward, out Left, magnitude, CollisionDetectionLayers.value)) {
                m_left = Left.distance / magnitude;
            } else {
                m_left = 0;
            }
            if (m_left > 0 && m_right < 0) {
                if (m_left > -m_right) {
                    m_right = 0;
                    if (steer > 0) {
                        if (m_left > steer) {
                            steer = m_left;
                        }
                    } else if (m_left > 0.4f) {
                        steer = m_left;
                    }
                } else if (-m_right > m_left) {
                    m_left = 0;
                    if (steer < 0) {
                        if (m_right < steer) {
                            steer = m_right;
                        }
                    } else if (m_right < -0.4f) {
                        steer = m_right;
                    }
                }
            }

Steer is set prior to raycast logic, and is attempting to steer towards the target with no attempt to avoid collisions.

Seek:

OR PURSUIT

Plus collision avoidance:

These are called steering behaviors and are pretty efficient.

1 Like

Yes, steering behaviors would be a good idea here.

If you like reading books, Programming AI By Example is relevent:

1 Like

I actually already took a look at those, and tried it, but the issue is that it is difficult to tell the police car when to prioritize avoiding obstacles and when to prioritize chasing the player. The police car will many times attempt to avoid obstacles that aren’t really a threat because the player will lead them away, and then take wrong turns that lead to an abrupt ending to the chase.

Tried RAIN AI? It’s free and uses custom navmesh solution (no idea if faster on mobile, though worth a try) and you can make AI by defining behavior trees.