- Home /
Scripting Grenade AI. Checking if Array Objects are close to eachother.
I'm working on an AI script and want to include AOE effects such as grenades. The idea.. is that if two or more enemies are within 4meters of eachother, the attack is aimed at one of them... or in the middle (depending on specific attack)
I could manually do this by saying "if Enemy[1] is near enough to Enemy[2] ... shoot inbetween..." but obviously that doesn't work as I have an unset number of playertargets.
Available Targets are listen in an Array... also.. there is a CurrentTarget variable.. so if nothing else I could compare all other members of array to the current targets position.
If it was testing for 3 people in close proximity how would it differ?
Answer by sparkzbarca · Nov 21, 2012 at 07:33 PM
heres roughly how you do it
raycastsphere to nearest enemy or whatever your target is. This is just your initial target your not going to FOR CERTAIN use that as the target for the grenade in the end.
That will give you an array which contains all objects within a certain radius of the enemy including the target himself.
Now what you do is you make the target point for the grenade equal to
gameobject[] enemytargets = physics.raycastsphere(origin is nearest enemy or whatever initial target, radius is your choice)
this will make enemytargets an array containing all the enemies
vector3 targetpoint = vector3.zero;
foreach (gameobject enemytarget in enemytargets){
targetpoint += enemytarget.transform.position;
}
targetpoint /= enemytargets.size();
what you have done with that code is taken the position of all the enemies and found the center by adding all the positions and dividing by the number of positions.
Now if there is only 1 enemy the center is the enemy so 1 enemy will resolve itself
multiple enemies will have the center point between all enemies found.
Make sure the radius is set right for the explosion.
You wouldnt want the radius so high that you throw the grenade in the middle 4 guys spaced so far apart none of them are hit.
technically speaking this will be a point above the ground. if you want to aim for "ground"
raycast with an origin as the targetpoint, the direction as -vector3.up so you cast straight down.
set the targetpoint to hit.point; that will give you the point on the ground in the center of all targets.
MARK AS ANSWERED and happy coding
This covers my alternative approach, to see if other enemies are nearest to my target.
Is there a way of checking proximity of all Array members with every other Array member? I believe the Target solution as you have answered might be best for most situations, but as a growing coder I'd like to know all angles.
you could do it, it not could but WOULD get really expensive with a large number of enemies.
enemyarray is also an array of type enemy which is the struct. it includes an list called nearbyenemies which is of type vector3 for this example since all it's doing is storing positions.
alternatly you could have enemyarray be an array of gameobjects and have each enemy gameobject have a script with nearbyenemies declared in it and get the attached component.
this will use gameobject arrays
foreach (enemy in enemyarray)
{
enemiesnearby = enemy.getcomponent<ScriptWithNearbyEnemiesList>().Nearbyenemies;
foreach (otherenemy in enemyarray)
{
if(otherenemy != enemy)
enemiesnearby.push(otherenemy);
}
//insert a sort here if you want
}
that will give each enemy an array attached to it that stores the position of nearby enemeis
you could iterate though that to get distance or something.
Honestly i'm not sure why you want this but hey its there. :)
mark as answered. :)
Your answer
Follow this Question
Related Questions
Nearest Enemy To Team (Without Crowding!) 1 Answer
More Efficient way? 1 Answer
Basic Tower Defense Tower AI 2 Answers
Array out of range 1 Answer
array of boxes 2 Answers