Need help in AI logic problem with multi target scenario!

Hi everyone there!!

So i’m trying to make a fancy behaviour for my basic follow target AI. Well lets say that i’m trying to go forward with Ai behaviour development and still can’t go beyond a basic follow & shoot AI. So i was trying to implement some kind of multi target method that allow the ai to face a multi target situation where there is more than just the player to shoot or attack at. And after a loot of efford for making this work. I came with a number of weird behaviours that i really have no idea how to fix or even approach.

Well going into the core of this. One of my biggest throuble with this multi target situation for the ai to face. Is no more and no less than “CanSeeTarget?” This is a boolean function i found dont remember where exactly. And work pretty well with only one target situation.

Here’s the code :

   function CanSeeTarget ():boolean{
        if (Vector3.Distance(transform.position, target.position) >attackRange)
        return false;
        var hit : RaycastHit;
        if (Physics.Linecast (EyePos.position, target.position,hit, SearchMask)){
        	lastVisibleTargetPosition = target.position;
        	return hit.transform == target;
        return false;

Sorry for the shit position of the lines of code but i can’t fix it. Well as i was saying… This is i think the main problem of the weird behaviour my ai has. And of course i almost forgot to explain what is all about that weird behaviour.

Well to be quick : Let’s say that for example you have a 5v5 NPC/AI situation in a shooting scenario. So they get close by a simple path function that i set up so they can “behave” as a “moba” kind of battle. But as soon as they start fight each other start the weird stuff .Where they stop shooting and run forward to the enemy “battle side” and they start to get stuck in fron of and running against each other. Of course not all of the 10 npc do this, but pretty much like 6 or 7 do it while the others can behave “normal”.

I figure out that this is kind of “i can’t see” situation. Where for some reason in a certain point they can’t see the chosen target wich is for sure one of the other side in front of them. This is not the first time i get this problem, the last year 2015 i was practicing on another project and found that same issue. That time i just eliminate the Linecast check and let just make sure the ai is close enough to engage. Wich is not a good way to go really. Now i want to find a way to fix this problem and be able to go forward with my game.

So now that i explain in detail my situation, come the question.

Any of you have a good idea of how can i possibly even approach such an issue?

I’m in the 4 o 5 day of trying to solve this shit problem. So in that time i was thinking very much and i get a number of pose but after all this time i think it going to worth the time asking here. Becouse to be honest i still have not a good clue on where to go exactly. And i’m get good number of clash points where i have to chose one solution or another and none of then is exempts of cons.

One of my initial thought was to same as in my old project, just eliminate the linecast check and go with distances. So if i do this the Ai will not care about if target is actually in fire line or not, as soon as they get close are a target. But of coruse this get a lot of bad situations like an ai shooting at a wall becouse cant figure that is nothing to shoot at there and so on.

Another one wich is what i’m trying to now but lead me to a more fancy code stuff. Is to check if the ai can see the target with linecast but if return false trigger another check. Wich make a loop over all npc in scene and check one by one wich one is more close to the AI. And finally check the distance of the pre-selected target and the attackrange. So if i can get this work it should fix the problem but even when im alredy write this it isn’t workin properly died. And that’s why i’m writing this to beging with.

It’s funny that after writing all this i’m start to think that i have to be fine with this last solution. But in case it wont let me know if you have a good way to approach this situation. It’s really frustrating and i’m going have to probably re write a 1 thousand line of code Ai wich is a low hit on the ball’s.

Hope i was explicit enough and delivered enough information, if not let me know. I’m not sharing the entire code just becouse of the lack of comment’s so if i share no one will understand it easily anyway xD. Thanks in advance for any response.

Maybe you could be less strict about deciding whether an enemy is visible or not. Otherwise, you can run into ridiculous situations where your AI has spotted an enemy, then just forget about it because your AI slightly turns his head away and don’t see the enemy anymore. One approach could give your AI some short term memory (just a timer), such that even when the enemy is not visible, it is still nonetheless a valid target with a known position.

Also, you’ve mentioned that you might need to rewrite a lot of code. I would advice to have a look at Behaviour Tree, it’s a great technique to handle complex (or simple) AIs and help you to keep your code neat and organized. Have a look at Panda BT ( The package also contains examples of patrol-chase-attack behaviours. I’m the author, so I would be glad to help if you have any question.

Well after a morning of trial and error i finally found the solution. It’s a mix of what @ericbegue sugest me and what i already though. I change the linecast check to see if the blocking object has the same tag as the target. And if it does it continue shooting, if not return false and the ai can chase giving me a more consistent behaviour. Now they rarely get stuck if another ai is blocking the view. And of course i apply the chase timer with a stored position of the last moment the enemy was visible. So now they are behaving as i spect them to do.

Thanks for the answer and don’t worry i’m going to check the panda behaviour any way. I have a lot of way to go before i have a completed and playable game so any helping tool or advice will be very much apreciated.

Thanks again!