# 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. Commented 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... Commented 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. Commented 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 Commented 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. Commented Jun 8, 2015 at 23:32

## 1 Answer

As discussed in the comments above, you may be over-engineering this. If you have ~12 characters who could possibly be attack targets, iterating over all 12 and checking the distance (or squared distance if you want to be extra frugal and avoid a square root) should take a negligible amount of time. In typical situations you'll spend more time waiting to get the character's position vector from memory than it takes to do this check.

Just make sure you have a pre-populated list of racers, rather than calling a slow Find()-type routine every time. These methods search through every object in your scene, and get slower the more content you have. You'll probably want such a list for keeping track of rankings during the race anyway, so this shouldn't add any extra overhead.

Your other checks, like figuring out if the racer is in a direction you can shoot, and whether there are any occluding obstacles in the way, can then be run for only those racers who are within your maximum range.

• Thanks much for it,I have implemented similar way what you have suggested.Basically what I did is, While iterating over list of characters , I checked if other player's rank is -1 or +1 of current player's , if yes then performed distance and other attack functions.So I guess that concludes this question.Again thank you for helping in. Commented Jun 9, 2015 at 18:04