- Home /
Suggestions for AI Selecting From Multiple Targets?
I am setting up AI state system for my game and I have all the normal functions that an action battle system would have: Pathfinding, Movement, and Attacking . This system of mind is working well (For a one player game).
However, I will be having the player be assisted by 3 other partner AI (using the same scripts). Now, the problem is, the enemy's are locked on to the player object only, and the partner AI are targeting only the one enemy entity selected in the inspector.
I was wondering on what approach I should go towards getting the enemy to select a target between the 3-4 players and vise-versa for the partner AI.
So far, I have code from my targeting system that allows the player to find all of the closest enemies and selects it as a target. I am not sure how to get the enemy to use this same function.
var closest : GameObject;
function FindClosestEnemy () : GameObject { // Find all game objects with tag Enemy enemyLocations = GameObject.FindGameObjectsWithTag("Enemy"); //var closest : GameObject; var distance = Mathf.Infinity; var position = transform.position; // Iterate through them and find the closest one for (var go : GameObject in enemyLocations) { var diff = (go.transform.position - position); var curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
return closest;
}
I would conclude that maybe the following would be appropriate to make an enemy/partner change there target?
-Closest Enemy -Health Checking -Stat Checking (IDK) -Death?
Would a timer suffice for changing targets?
I would also need the Partners to set the player as there target when I'm fleeing from enemies or when the enemies are all dead.
Is it a multiplayer game, using the built-in networking?
No, this is a one-four player game running one computer (for now)
may i know if where can i put on this script my attack codes?? please.
Answer by StephanK · Feb 11, 2011 at 08:43 AM
Basically you can use the same function for enemy AI. You will just have to modify the tag it's searching for. I would just change the signature of the function to FindClosestEnemy(string tag) and use the same function for players and enemies.
Facepalm That takes care of the enemies finding the locations of the players. But now, I just need to figure out the rest of the logic.
I'd probably implement this as some kind of filter, where each filter is basically a function that takes a list and returns a list of enemies after filtering out certain conditions. So you could do something like this: 1. find relevant enemies. 2. filter out enemies too far away. 3. remove enemies that are "too strong" 4. find the closest of the remaining enemies
Answer by bananaboy · Sep 12, 2011 at 09:22 PM
Hello there, what about the attack script to be include to that script so that when it finds multiple targets it would immediately attack them by projectiles. Please help me with this. because i'm having trouble in using this script.
Answer by Ziron999 · Feb 01, 2015 at 02:06 PM
void DistanceToTarget()
{
//Sort so nearest enemy is always Enemies[0]
PlayerUnits.Sort(delegate( Transform t1, Transform t2){
return Vector3.Distance(t1.position,transform.position).CompareTo(Vector3.Distance(t2.position,transform.position));
});
}
void TargetedEnemy()
{
DistanceToTarget();
//after sorted nearest enemy will be 0
SelectedTarget = PlayerUnits[0];
}
This can handle around 80 units for both players/enemies if done right with collisions. If someone knows of a better optimized way please let me know!
Your answer
Follow this Question
Related Questions
SphereCastAll sees for miles 2 Answers
Following the next item in an array 0 Answers
2D Random AI 1 Answer
free roaming enemy ai 1 Answer