# Kart Racing Attack AI

I am developing a kart racing game. In it, the players fight with each other while racing.

There were several ways I tried to add attack AI to the enemies and the player.

1. When the user clicks on the attack function, it throws raycasts in the certain area; if enemy is found then the user gets presented with the weapon and auto attacks occurred.

Later I moved on from this code for two reasons, first very dirty and highly unoptimised and second it's only able to calculate limited distances (not for missile weapons which are long range); a new approach needed to be implemented.

1. After thinking and researching I tried Physics.Overlap; this did solve my optimization problem with short range weapons but still the problem of long range persists.

2. Thinking around more I came up with other ways to implement long range attack, i.e., by spawning one collider just a little behind the player and it will go foreword until a certain distance and if the enemy gets found it will inform the player and the weapon will be spawned and attacked.

Though that third approach looks good so far, the problem arises when the game gets high pace as the collider sometimes do not catch the other players or get too slow to catch the other players and result of that buggy attack look.

So I would like to know: is there any other way to implement long range attacks considering it is a kart racing game?

Any help will be appreciated

• How many characters do you expect to have? If it's on the order of dozens or less, you could probably just iterate over every possible target character and check if they're in range, without feeling any performance hit, and without distractions from other physics objects in the scene. Jun 8, 2015 at 22:27
• It depends on level type from 4-12.You have point but isnt it will add over head ,suppose level has 8 characters,now each character, (enemy+player) iterating over each other so that would be 8*8 = 64 iterations per frame... Jun 8, 2015 at 22:32
• It's only 8x8 if everyone is attacking in the same frame. If you only do the check when a character tries to attack, and stagger AI attack attempts, the typical hit will be far less. Also consider that each character doing a large-radius sphere check may be more expensive than checking squared distance to a small number of other characters. If you have a system that tracks race position, you can even limit the checks to the first 2-3 racers ahead of the character. If that sounds reasonable I can expand this into an answer. Jun 8, 2015 at 22:38
• thats very good points....sounds reasonable too,the only reason I was avoiding Distance calculation as I read its too slow.....coming to your points ,when user clicks attack button, let random value get pass to enemy after which enemy will perform its attck so it will feel natural too? m I on right path?.....Feel free to expand this into an answer with lil more spotlights...thanks Jun 8, 2015 at 22:48
• Distance calculation is basically sqrt((x1 - x2)^2 + (y1 - y2)^2). To a computer calculating that is practically nothing, especially compared to 2D (or 3D) collision detection. Jun 8, 2015 at 23:32