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.